ソケットから特定のバイト数を読み取ろうとしています。私のサーバーは送信しています:
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(...)
{
}
}