问题
I've been trying to write a client-server application using boost::asio, overall the application works great but I've stumbled over a problem regarding the data provided by 'WriteHandler' ( async_write function) when the connoction (between client <> server) is droped by a firewalll or by manualy disabling a newtwork card.
Here is the code snippet:
void write(const CommunicatorMessage & msg, std::function<void(bool)> writeCallback)
{
io_service.dispatch(
[this, msg, writeCallback]()
{
boost::asio::async_write(socket, boost::asio::buffer(msg.data(), msg.length()), [this, msg, writeCallback](boost::system::error_code ec, std::size_t length)
{
writeCallback(ec != 0);
if (!ec)
{
LOG_DEBUG("Number of bytes written into the buffer: " << length << " Error code: " << ec);
LOG_DEBUG("Successfully send msg: " << msg);
}
else
{
LOG_ERROR("Failed sending msg: " << msg);
throw std::exception(ec.message().c_str());
}
});
});
}
The data from the Writehandler is valid (the error code is 0, bytes_transferred are correct), even if the data doesn't arrive on the server. I've tracked with Wireshark the whole flow( here's a link with the a screenshot Link)
回答1:
When async_write send you success it means it sucessfully written to kernel's buffer. But, its not guaranteed packet was delivered.
来源:https://stackoverflow.com/questions/19159829/writehandler-from-boostasioasync-write-doesnt-work-properly-when-connection