我正在开发一个客户端 - 服务器应用程序,其中两方都使用boost :: asio。 我正在尝试通过TCP发送大量数据(356 kb) 在服务器端,我写道:
boost::asio::async_write(Msocket,
boost::asio::buffer(sendBuffer,dataLen),
boost::bind(&ServerSession::onDataWrite,
this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_trasferred));
onDataWrite很简单:
void ServerSession::onDataWrite(const boost::system::error_code& error, const std::size_t bytesSent) {
if (error) {
std::cout << "Error " << error << " while sending data" << std::endl;
}
}
在客户端:
int readSize = ...; // defined from msg header, in this case equals 300 kbytes.
boost::asio::async_read(*Msocket,
boost::asio::buffer(recvBuffer, 50*1024*1024),
boost::asio::transfer_exactly(readSize),
boost::bind(&ClientSession::onDataRead,
this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_trasferred,
readSize));
onDataRead是:
void ClientSession::onDataRead(const boost::system::error_code& error, const std::size_t bytesRecvd, const int readSize) {
if (error || bytesRecvd != readSize) {
std::cout << "Error " << error << " while getting data, expect " << readSize <<", got " << bytesRecvd << std::endl;
}
}
在写入期间,服务器端打印
Error system:10014 while sending data
客户打印
Error system:0 while getting data, expect 393216, got 131064
131064 = 128kb - 8个字节的标题。
看起来这个128-kb问题是由发送/接收缓冲区溢出引起的。但是我觉得Boost会照顾那些缓冲区本身,对我来说是透明的。 我有什么误解?