Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ boost::asio::async#u write不';t连接断开时无法正常工作(防火墙/手动断开网络连接)_C++_Boost_Handler_Boost Asio - Fatal编程技术网

C++ boost::asio::async#u write不';t连接断开时无法正常工作(防火墙/手动断开网络连接)

C++ boost::asio::async#u write不';t连接断开时无法正常工作(防火墙/手动断开网络连接),c++,boost,handler,boost-asio,C++,Boost,Handler,Boost Asio,我一直在尝试使用boost::asio编写客户机-服务器应用程序,总的来说,该应用程序工作得很好,但当防火墙或手动禁用新工作卡断开连接(客户机-服务器之间)时,我遇到了一个关于“WriteHandler”(异步写入函数)提供的数据的问题 以下是代码片段: void write(const CommunicatorMessage & msg, std::function<void(bool)> writeCallback) { io_se

我一直在尝试使用boost::asio编写客户机-服务器应用程序,总的来说,该应用程序工作得很好,但当防火墙或手动禁用新工作卡断开连接(客户机-服务器之间)时,我遇到了一个关于“WriteHandler”(异步写入函数)提供的数据的问题

以下是代码片段:

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());
                    }
                });
            });
        }
void write(const CommunicatorMessage&msg,std::function writeCallback)
{
io_服务调度(
[this,msg,writeCallback]()
{
boost::asio::async_write(套接字,boost::asio::buffer(msg.data(),msg.length()),[this,msg,writeCallback](boost::system::error_code ec,std::size_t length)
{
回写(ec!=0);
如果(!ec)
{

LOG_DEBUG(“写入缓冲区的字节数:当异步写入发送成功时,意味着它成功写入内核缓冲区。但是,它的数据包没有得到保证。

通常,你无法通过这种方式检测到网络断开(阅读以下线程:)。您需要在应用程序中实现某种心跳。很好,谢谢您的澄清。我需要发送许多数据包,我需要有一个订单,是否有方法在发送下一个数据包之前检查服务器是否接收到数据包……等等?提前谢了。@lerys您的数据包将以相同的顺序发送和接收r,因为它是TCP流。但是,在发生重新传输超时之前,您无法知道数据包已丢失。在这种情况下,将使用
boost::asio::error::eof
boost::asio::error::breaked_pipe
错误代码调用读/写处理程序。