WriteHandler from boost::asio::async_write doesn't work properly when connection is dropped (firewall / manual disconecting the network)

穿精又带淫゛_ 提交于 2021-02-05 08:14:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!