我正在尝试从套接字读取特定数量的字节。我的服务器正在发送:
1) byte[0] - 消息的长度 2) byte[1:N] - 实际的消息
如何读取第一个字节,然后使用 boost::asio::ip::tcp::socket::read 读取剩余字节?这是代码片段:
// receive data through the socket
void TCPTestClient::ReceiveData( )
{
try
{
boost::system::error_code error;
boost::asio::streambuf receivedStreamBuffer;
// reserve 512 bytes in output sequence
boost::asio::streambuf::mutable_buffers_type bufs =receivedStreamBuffer.prepare( 512 );
boost::asio::read( m_socket,
bufs,
error );
// transfer the buffer contents to string
std::istream is( &receivedStreamBuffer );
is >> m_receivedMessageStr;
// throw exception if error occurred
if ( error )
{
throw NetworkTestFailedException( error.message() );
}
}
catch(...)
{
}
}