How to use TCP keep_alive property to get notified on the event of a unresponsive peer?

亡梦爱人 提交于 2019-12-13 02:59:31

问题


Scenario:
I have a client and server written using boost::asio 1.63. Generally the connection and communication part works well and great.

I have written a Watchdog on both sides which send dummy packets to peers in an interval of 2 seconds each. The objective of the watchdog is that the concerned peer reports a connection error if it does not receive the dummy packet it is expecting in the next 2 seconds. This is even more important for me because it might happen the 2 peers are not transacting packets for any user purpose but each of them is required to report a connection error if any of the peer goes down. The peer can go down even because of a kernel crash in which case it would not be possible for that peer to send a message. This is a classic problem of course which exists even beyond asio and TCP.

My Watchdog works perfectly well. No issues at all.

But, recently I read about the keep_alive feature in sockets. I tried out the following code and seems like I can a property called keep_alive on the TCP socket by getting the native handle to the socket from within my code using boost::asio.

boost::asio::io_service      ioService;
boost::asio::ip::tcp::socket mySocket(ioService);

int on = 1;
int delay = 120;
setsockopt(mySocket.native_handle(), SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
setsockopt(mySocket.native_handle(), IPPROTO_TCP, TCP_KEEPALIVE, &delay, sizeof(delay));

Question:
Above code compiles well on macOS, Linux and iOS. That looks great. But, how do I benefit from this? Does this give me a callback or event when the peer goes down? Does this free me up from writting the Watchdog that I described above?

I have used boost::asio::async_connect to connect to the peer. Can I get a callback to my connectionHandler when the perr goes down after the defined timeout interval?

Having set the keep_alive options, how do I then get to know that my peer is not responding anymore?


回答1:


If the disconnetion was detected when an async operation is pending, your socket's completion handler will be invoked with the appropriate error code.

The problem is that TCP keep_alive option doesn't not always detect disconnects.

In general, there is no reliable way to detect sudden disconnection, other than by implementing application-level ping/heartbeat.

You can also see this thread.



来源:https://stackoverflow.com/questions/58689857/how-to-use-tcp-keep-alive-property-to-get-notified-on-the-event-of-a-unresponsiv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!