从套接字读取时boost.asio错误

从套接字读取时boost.asio错误,boost,boost-asio,Boost,Boost Asio,客户端的以下代码: typedef boost::array<char, 10> header_packet; header_packet header; boost::system::error_code error; ... /** send header */ boost::asio::write( _socket, boost::asio::buffer(header, header.size()), boost::asi

客户端的以下代码:

  typedef boost::array<char, 10> header_packet;
  header_packet header;
  boost::system::error_code error;
  ...
  /** send header */
  boost::asio::write(
     _socket,
     boost::asio::buffer(header, header.size()),
     boost::asio::transfer_all(),
     error
  );
  /** send body */
  boost::asio::write(
     _socket,
     boost::asio::buffer(buffer, buffer.length()),
     boost::asio::transfer_all(),
     error
  );
typedef boost::数组头\u数据包;
报头\分组报头;
boost::system::error\u code error;
...
/**发送头*/
boost::asio::write(
_插座,
boost::asio::buffer(header,header.size()),
boost::asio::transfer_all(),
错误
);
/**发送机构*/
boost::asio::write(
_插座,
boost::asio::buffer(buffer,buffer.length()),
boost::asio::transfer_all(),
错误
);
服务器的名称:

   struct header {
      boost::uint32_t header_length;
      boost::uint32_t id;
      boost::uint32_t body_length;
   };
   static header unpack_header(const header_packet& data) {
      header hdr;
      sscanf(data.data(), "%02d%04d%04d", &hdr.header_length, &hdr.id, &hdr.body_length);
      return hdr;
   }

void connection::start() {
   boost::asio::async_read(
      _socket,
      boost::asio::buffer(_header, _header.size()),
      boost::bind(
         &connection::read_header_handler,
         shared_from_this(),
         boost::asio::placeholders::error
      )
   );
}

/***************************************************************************/

void connection::read_header_handler(const boost::system::error_code& e) {
   if ( !e ) {
      std::cout << "readed header: " << _header.c_array() << std::endl;
      std::cout << constants::unpack_header(_header);
      boost::asio::async_read(
         _socket,
         boost::asio::buffer(_body, constants::unpack_header(_header).body_length),
         boost::bind(
            &connection::read_body_handler,
            shared_from_this(),
            boost::asio::placeholders::error
         )
      );
   } else {
      /** report error */
      std::cout << "read header finished with error: " << e.message() << std::endl;
   }
}

/***************************************************************************/

void connection::read_body_handler(const boost::system::error_code& e) {
   if ( !e ) {
      std::cout << "readed body: " << _body.c_array() << std::endl;
      start();
   } else {
      /** report error */
      std::cout << "read body finished with error: " << e.message() << std::endl;
   }
}
struct头文件{
boost::uint32_t头长度;
boost::uint32\u t id;
boost::uint32体长;
};
静态头文件解包头文件(常量头文件包和数据){
标题hdr;
sscanf(data.data(),%02d%04d%04d“,&hdr.header\u length,&hdr.id,&hdr.body\u length);
返回hdr;
}
无效连接::开始(){
boost::asio::异步读取(
_插座,
boost::asio::buffer(_header,_header.size()),
boost::bind(
&连接::读取\u头\u处理程序,
从_this()共享了_,
boost::asio::占位符::错误
)
);
}
/***************************************************************************/
无效连接::读取头处理程序(常量boost::系统::错误代码&e){
如果(!e){
问题解决了


在asio::write()中发送序列化结果时,我的代码出错。因此服务器无法读取任何内容。

解决方案是什么?将其作为答案发布,您可以回答自己的问题。