c++ - boost 从 tcp 套接字接收数据

标签 c++ sockets boost tcp boost-asio

我正在尝试从套接字读取特定数量的字节。我的服务器正在发送:

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(...)
        {

        }
    }

最佳答案

您需要为单字节 header 准备一个缓冲区,然后为消息准备另一个缓冲区。一个简化的例子可能是

boost::asio::read(                                                                                                                       
        m_socket,                                                                                                                        
        receivedStreamBuffer.prepare(1),                                                                                                 
        error                                                                                                                            
        );                                                                                                                               

if ( error ) {                                                                                                                           
    std::cerr << "Read header failed: " << boost::system::system_error(error).what() << std::endl;                                       
    return;                                                                                                                              
}                                                                                                                                        
receivedStreamBuffer.commit(1);                                                                                                          
std::istream header( &receivedStreamBuffer );                                                                                            
uint8_t size;                                                                                                                            
header >> size;                                                                                                                          

// reserve message size in output sequence                                                                                               
boost::asio::read(                                                                                                                       
        m_socket,                                                                                                                        
        receivedStreamBuffer.prepare( size ),                                                                                                 
        bufs,                                                                                                                            
        error                                                                                                                            
        );                                                                                                                               
if ( error ) {                                                                                                                           
    std::cerr << "Read message failed: " << boost::system::system_error(error).what() << std::endl;                                      
    return;                                                                                                                              
}                                                                                                                                        
receivedStreamBuffer.commit( size );                                                                                                     

// transfer the buffer contents to string                                                                                                
std::istream is( &receivedStreamBuffer );                                                                                                
is >> m_receivedMessageStr;    

关于c++ - boost 从 tcp 套接字接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14324060/

相关文章:

使用默认参数的 C++ 函数模板

c++ - 当返回值是class或class<class>或class<class, class>等时如何使用enable_if?

c++ - 将围绕sockaddr_storage和sockaddr_in进行转换将破坏严格的别名

c++ - boost crc 每次产生不同的输出

c++ - 标记化字符串,接受 CPP 中给定字符集之间的所有内容

c++ - ifstream 不会在下一次迭代中打开文件

c++ - 在无效循环 cout 操作期间,元素自动插入到 std::map

java - 最小化 Tomcat "per connection"内存占用

python - 发送的scapy包收不到

c++ - 用替代运算符提振 spirit '|' 失败!当有两个可能的规则时