<bdo id='iQwAB'></bdo><ul id='iQwAB'></ul>

<small id='iQwAB'></small><noframes id='iQwAB'>

<tfoot id='iQwAB'></tfoot><legend id='iQwAB'><style id='iQwAB'><dir id='iQwAB'><q id='iQwAB'></q></dir></style></legend>
      1. <i id='iQwAB'><tr id='iQwAB'><dt id='iQwAB'><q id='iQwAB'><span id='iQwAB'><b id='iQwAB'><form id='iQwAB'><ins id='iQwAB'></ins><ul id='iQwAB'></ul><sub id='iQwAB'></sub></form><legend id='iQwAB'></legend><bdo id='iQwAB'><pre id='iQwAB'><center id='iQwAB'></center></pre></bdo></b><th id='iQwAB'></th></span></q></dt></tr></i><div id='iQwAB'><tfoot id='iQwAB'></tfoot><dl id='iQwAB'><fieldset id='iQwAB'></fieldset></dl></div>

        如何检测 TCP 连接中的网线已被拔掉?

        Howto detect that a network cable has been unplugged in a TCP connection?(如何检测 TCP 连接中的网线已被拔掉?)
      2. <i id='9ur6s'><tr id='9ur6s'><dt id='9ur6s'><q id='9ur6s'><span id='9ur6s'><b id='9ur6s'><form id='9ur6s'><ins id='9ur6s'></ins><ul id='9ur6s'></ul><sub id='9ur6s'></sub></form><legend id='9ur6s'></legend><bdo id='9ur6s'><pre id='9ur6s'><center id='9ur6s'></center></pre></bdo></b><th id='9ur6s'></th></span></q></dt></tr></i><div id='9ur6s'><tfoot id='9ur6s'></tfoot><dl id='9ur6s'><fieldset id='9ur6s'></fieldset></dl></div>

              <tbody id='9ur6s'></tbody>

              1. <small id='9ur6s'></small><noframes id='9ur6s'>

                  <bdo id='9ur6s'></bdo><ul id='9ur6s'></ul>

                • <tfoot id='9ur6s'></tfoot>
                  <legend id='9ur6s'><style id='9ur6s'><dir id='9ur6s'><q id='9ur6s'></q></dir></style></legend>
                  本文介绍了如何检测 TCP 连接中的网线已被拔掉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 C++ 网络应用程序,它接受来自客户端的 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.

                  我无法控制客户端,也无法让他们发送诸如ping"之类的内容.我的服务器确实向客户端发送了一个ping"数据包(但他们不会发送响应),但即使拔掉电缆 write() 也会返回正确的字节数(我看到 TCP 堆栈发送重试Wireshark 中的数据包).

                  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).

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

                  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.

                  我需要它在 Windows 和 Linux 上运行.

                  I need this to work on Windows and on Linux.

                  推荐答案

                  遗憾的是,无法将另一端被拔出的电缆与任何其他丢包原因区分开来.话虽如此,您可以将另一端的连接丢失近似为在足够长的时间段内(例如 T)发生的无限数据包丢失".TCP 跟踪数据包丢失,因此执行此操作的一般方法是:

                  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:

                  • 获取连接中未确认的字节数(假设是 B)
                  • 发送数据,大小 = N
                  • 设置超时时间 = T,当它触发时,再次检查未确认字节数.如果是B+N,则假设对方已经失去连接.此时,您可以尝试 ICMP echo 来验证您的假设.

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

                  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.

                  另一种方法(即近似跟踪连接丢失)是通过 RAW 套接字.打开 RAW 套接字并对其进行过滤以仅接收连接的 TCP 流量.然后,与其从 TCP 获取信息以确定您是否从另一端获取任何信息,只需等待从另一端接收任何 数据包即可.如果你在规定的时间内得到了一些东西,那么这意味着peer仍然是up的.

                  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 连接中的网线已被拔掉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)

                  <i id='XI6S0'><tr id='XI6S0'><dt id='XI6S0'><q id='XI6S0'><span id='XI6S0'><b id='XI6S0'><form id='XI6S0'><ins id='XI6S0'></ins><ul id='XI6S0'></ul><sub id='XI6S0'></sub></form><legend id='XI6S0'></legend><bdo id='XI6S0'><pre id='XI6S0'><center id='XI6S0'></center></pre></bdo></b><th id='XI6S0'></th></span></q></dt></tr></i><div id='XI6S0'><tfoot id='XI6S0'></tfoot><dl id='XI6S0'><fieldset id='XI6S0'></fieldset></dl></div>

                  <tfoot id='XI6S0'></tfoot>

                      <tbody id='XI6S0'></tbody>
                    • <legend id='XI6S0'><style id='XI6S0'><dir id='XI6S0'><q id='XI6S0'></q></dir></style></legend>

                      <small id='XI6S0'></small><noframes id='XI6S0'>

                        <bdo id='XI6S0'></bdo><ul id='XI6S0'></ul>