The following code of the client:
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
);
of the server:
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;
}
}
On the server side the method read_header_handler() is called, but the method read_body_handler() is never called. Though the client has written down the data in a socket. The header is readed and decoded successfully. What's the error?