如何使用TCP keep_alive属性在无响应的对等事件时得到通知? [英] How to use TCP keep_alive property to get notified on the event of a unresponsive peer?

查看:205

如果在异步操作待处理时检测到断开连接,则会使用适当的错误代码调用套接字的完成处理程序./p>

问题在于TCP keep_alive选项并不总是检测到断开连接.

通常,除了实现应用程序级ping/心跳之外,没有可靠的方法来检测突然断开连接.

您还可以看到此线程.

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?

解决方案

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.

这篇关于如何使用TCP keep_alive属性在无响应的对等事件时得到通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆