<legend id='sVxAl'><style id='sVxAl'><dir id='sVxAl'><q id='sVxAl'></q></dir></style></legend>
      • <bdo id='sVxAl'></bdo><ul id='sVxAl'></ul>
    1. <tfoot id='sVxAl'></tfoot>
      <i id='sVxAl'><tr id='sVxAl'><dt id='sVxAl'><q id='sVxAl'><span id='sVxAl'><b id='sVxAl'><form id='sVxAl'><ins id='sVxAl'></ins><ul id='sVxAl'></ul><sub id='sVxAl'></sub></form><legend id='sVxAl'></legend><bdo id='sVxAl'><pre id='sVxAl'><center id='sVxAl'></center></pre></bdo></b><th id='sVxAl'></th></span></q></dt></tr></i><div id='sVxAl'><tfoot id='sVxAl'></tfoot><dl id='sVxAl'><fieldset id='sVxAl'></fieldset></dl></div>

      <small id='sVxAl'></small><noframes id='sVxAl'>

      如何检测 boost tcp 套接字何时断开连接

      时间:2023-07-19
        <tbody id='7MITn'></tbody>

      <small id='7MITn'></small><noframes id='7MITn'>

        <i id='7MITn'><tr id='7MITn'><dt id='7MITn'><q id='7MITn'><span id='7MITn'><b id='7MITn'><form id='7MITn'><ins id='7MITn'></ins><ul id='7MITn'></ul><sub id='7MITn'></sub></form><legend id='7MITn'></legend><bdo id='7MITn'><pre id='7MITn'><center id='7MITn'></center></pre></bdo></b><th id='7MITn'></th></span></q></dt></tr></i><div id='7MITn'><tfoot id='7MITn'></tfoot><dl id='7MITn'><fieldset id='7MITn'></fieldset></dl></div>
        1. <legend id='7MITn'><style id='7MITn'><dir id='7MITn'><q id='7MITn'></q></dir></style></legend>
            • <tfoot id='7MITn'></tfoot>

                <bdo id='7MITn'></bdo><ul id='7MITn'></ul>
                本文介绍了如何检测 boost tcp 套接字何时断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                假设我有一个套接字:

                std::shared_ptr<tcp::socket> socket( new tcp::socket(acceptor.get_io_service()) );
                acceptor.async_accept( *socket, std::bind( handleAccept, this, std::placeholders::_1, socket, std::ref(acceptor)) );
                

                我将一个weak_ptr存储到容器中的所述套接字中.我需要这个是因为我想允许客户端请求其他客户端的列表,以便他们可以相互发送消息.

                And I store a weak_ptr to the said socket in a container. I need this because I want to allow clients to request for a list of other clients, so they can send messages to each other.

                clients_.insert(socket);  // pseudocode
                

                然后我运行一些异步操作

                Then I run some async operations

                socket->async_receive( boost::asio::buffer(&(*header), sizeof(Header))
                    , 0
                    , std::bind(handleReceiveHeader, this, std::placeholders::_1, std::placeholders::_2, header, socket));
                

                如何检测连接何时关闭,以便从容器中删除套接字?

                How do I detect when the connection is closed so I can remove my socket from the container?

                clients_.erase(socket); // pseudocode
                

                推荐答案

                TCP 套接字断开通常在 asio 中由 eofconnection_reset<发出信号/代码>.例如

                A TCP socket disconnect is usually signalled in asio by an eof or a connection_reset. E.g.

                  void async_receive(boost::system::error_code const& error,
                                     size_t bytes_transferred)
                  {
                    if ((boost::asio::error::eof == error) ||
                        (boost::asio::error::connection_reset == error))
                    {
                      // handle the disconnect.
                    }
                    else
                    {
                       // read the data 
                    }
                  }
                

                我使用 boost::signals2 来表示断开连接,尽管您始终可以将指向函数的指针传递给您的套接字类,然后调用它.

                I use boost::signals2 to signal the disconnect although you can always pass a pointer to a function to your socket class and then call that.

                请注意您的套接字和回调生命周期,请参阅:boost-异步函数和共享指针

                Be careful about your socket and callback lifetimes, see: boost-async-functions-and-shared-ptrs

                这篇关于如何检测 boost tcp 套接字何时断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <tbody id='rjIgv'></tbody>

                1. <small id='rjIgv'></small><noframes id='rjIgv'>

                    • <bdo id='rjIgv'></bdo><ul id='rjIgv'></ul>
                      <tfoot id='rjIgv'></tfoot>
                        <i id='rjIgv'><tr id='rjIgv'><dt id='rjIgv'><q id='rjIgv'><span id='rjIgv'><b id='rjIgv'><form id='rjIgv'><ins id='rjIgv'></ins><ul id='rjIgv'></ul><sub id='rjIgv'></sub></form><legend id='rjIgv'></legend><bdo id='rjIgv'><pre id='rjIgv'><center id='rjIgv'></center></pre></bdo></b><th id='rjIgv'></th></span></q></dt></tr></i><div id='rjIgv'><tfoot id='rjIgv'></tfoot><dl id='rjIgv'><fieldset id='rjIgv'></fieldset></dl></div>

                      1. <legend id='rjIgv'><style id='rjIgv'><dir id='rjIgv'><q id='rjIgv'></q></dir></style></legend>