How do I cleanly reconnect a boost::socket following a disconnect?

前端 未结 5 578
[愿得一人]
[愿得一人] 2020-12-08 07:13

My client application uses a boost::asio::ip::tcp::socket to connect to a remote server. If the app loses connection to this server (e.g. due to the server cras

5条回答
  •  青春惊慌失措
    2020-12-08 07:54

    Since C++11 you can write:

    decltype(socket)(std::move(socket));
    // reconnect socket.
    

    The above creates a local instance of socket's type move constructing it from socket.

    Before the next line, the unnamed local instance is destructed, leaving socket in a "freshly constructed from io_service" state.

    See: https://www.boost.org/doc/libs/1_63_0/doc/html/boost_asio/reference.html#boost_asio.reference.basic_stream_socket.basic_stream_socket.overload5

提交回复
热议问题