如何检测网络电缆已在TCP连接中拔出? [英] Howto detect that a network cable has been unplugged in a TCP connection?

查看:304
本文介绍了如何检测网络电缆已在TCP连接中拔出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++网络应用程序接受来自客户端的TCP连接,然后等待套接字,直到客户端决定发送数据(有时他们不会发送任何东西很长时间,这是OK)。



当客户端崩溃或机器关闭时,它主要检测错误情况,但是当客户端的网络电缆被拔下时,需要很多分钟才能注意到,我希望它注意到这种情况



我无法控制客户端,我不能让他们发送类似ping的消息。我的服务器发送一个ping数据包到客户端(但他们不会发送响应),但即使电缆被拔掉write()返回正确的字节数(我看到TCP堆栈发送重试在Wireshark中的数据包)。



注意丢失连接的最好方法是什么?如果我可以在write()调用上检测到它,这将是最方便的。



我需要这个工作在Windows和Linux上。

$ b $不幸的是,没有办法区分在另一端被拉出的电缆与任何其他丢包原因。尽管如此,你可以估计在另一端的连接丢失,因为不确定的分组丢失发生在足够长的时间段(说T)。 TCP跟踪丢包,所以一般的做法是:




  • 获取连接中未压缩的字节数)

  • 发送数据,size = N

  • 设置超时= T,当它触发时,再次检查未包装的字节数。如果它是B + N,则假定另一方已经失去连接。此时,您可以尝试使用ICMP回应来验证您的假设。



获取连接的TCP特定信息不是标准接口在UNIX上,绝对不是可移植到Windows的东西。在Linux上,有一个名为TCP_INFO的套接字选项,您可以通过getsockopt()调用它。 Google应该给你一些例子。我不知道在Windows上是否有一个等效的选项。



另一种方法(即近似跟踪连接丢失)是通过RAW套接字。打开RAW套接字并过滤它以仅接收连接的TCP流量。然后,不是从TCP获取信息,而是确定是否从另一端获取任何内容,只需等待从另一端接收任何 包。如果你在规定的时间内得到某些东西,那么这意味着同伴还在。


I have a C++ networking application that accepts TCP connections from clients and then waits on the socket until the client decides to send data (sometimes they won't send anything for a long time and thats OK).

It mostly detects error conditions when clients crash or machines are turned off, but it takes many minutes to notice when the network cable to the client has been unplugged and I would prefer it to notice this condition as soon as possible.

I don't have control over the clients and I can't make them send something like a "ping". My server does send out a "ping" packet to the to the clients (but they won't send a response), but even when the cable is unplugged write() returns the correct number of bytes (I see the TCP stack sending retry packets in Wireshark).

What is the best way to notice the loss of connection ? It would be most convenient if I could detect it on the write() call.

I need this to work on Windows and on Linux.

解决方案

Unfortunately, there is no way to distinguish the cable being pulled out at the other end from any other reason for packet loss. Having said that, you can approximate loss of connectivity at the other end as "indefinite packet loss" occurring over a sufficiently long period of time (say T). TCP tracks packet loss, so the general approach for doing this would be:

  • Get the number of unacked bytes in the connection (say it's B)
  • send data, size = N
  • Set a timeout = T, when it fires, check the number of unacked bytes again. If it's B+N, then assume that the other side has lost connectivity. At this point, you could try ICMP echo to verify your assumption.

Getting TCP-specific information for a connection is not a standard interface on UNIX, and definitely not something portable to Windows. On Linux, there's a socket option called TCP_INFO, which you can call via getsockopt(). Google should give you some examples. I don't know if there's an equivalent option on Windows.

Another way to do this (i.e. approximate tracking of connectivity loss) is via RAW sockets. Open a RAW socket and filter it to receive only TCP traffic for your connection. Then rather than fetching information from TCP to determine if you are getting anything from the other end, simply wait to receive any packet from the other side. If you get something in the stipulated period, then it means the peer is still up.

这篇关于如何检测网络电缆已在TCP连接中拔出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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