1
我想从套接字读取特定数量的字节。我的服务器发送:Boost从TCP套接字接收数据
1)字节[0] - 长度消息 2)字节的[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(...)
{
}
}