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

前端 未结 5 566
[愿得一人]
[愿得一人] 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:55

    I've done something similar using Boost.Asio in the past. I use the asynchronous methods, so a reconnect is typically letting my existing ip::tcp::socket object go out of scope, then creating a new one for calls to async_connect. If async_connect fails, I use a timer to sleep a bit then retry.

提交回复
热议问题