How to check if socket is closed in Boost.Asio?

后端 未结 6 1801
难免孤独
难免孤独 2020-12-11 04:17

What is the easiest way to check if a socket was closed on the remote side of the connection? socket::is_open() returns true even if it is closed on the remote

6条回答
  •  庸人自扰
    2020-12-11 05:02

    boost::asio::socket_base::keep_alive keepAlive(true);
    peerSocket->set_option(keepAlive);
    

    Enable keep alive for the peer socket. Use the native socket to adjust the keepalive interval so that as soon as the connection is closed the async_receive handler will get EOF while reading.

    Configuring TCP keep_alive with boost::asio

提交回复
热议问题