当断开连接时,boost :: asio :: async_write的WriteHandler无法正常工作(防火墙/手动断开网络连接) [英] WriteHandler from boost::asio::async_write doesn't work properly when connection is dropped (firewall / manual disconecting the network)
问题描述
我一直在尝试使用boost :: asio编写客户端-服务器应用程序,总体上该应用程序运行良好,但是当连接(客户端<>服务器之间)之间的连接被防火墙或手动禁用newtwork卡丢弃.
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.
这是代码段:
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());
}
});
});
}
即使数据没有到达服务器,来自Writehandler的数据也是有效的(错误代码为0,bytes_transferred是正确的).我已经用Wireshark跟踪了整个流程(这是一个屏幕截图的链接链接)>
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)
推荐答案
当 async_write 向您发送成功时,表示已成功写入内核缓冲区.但是,它的不能保证的数据包已传递.
When async_write send you success it means it sucessfully written to kernel's buffer. But, its not guaranteed packet was delivered.
这篇关于当断开连接时,boost :: asio :: async_write的WriteHandler无法正常工作(防火墙/手动断开网络连接)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!