winsock-recv-网络连接问题
在我的 C++ 应用程序中,我在循环中使用 recv
函数。 我想通过从 recv 函数获取负值来识别网络连接问题。
我在测试中可以看到的一件事是,当我断开网络电缆时,我可以等待大约一分钟,直到我看到我的应用程序得到了这个负值。
您知道 C++ 代码需要多长时间才能知道发生网络连接问题吗?这次我能应付得来吗?
In my C++ application I'm using the recv
function in a loop.
I want to identify a network connection issue by getting a negative value from recv function.
The one thing I can see in my tests is that when I'm getting off the network cable, I can wait for ~minute till I can see that my application got this negative value.
Do you know what is the time duration for the C++ code to know that a network connection issue occur? Can I manage this time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在仅 TCP 级别上没有明智的方法来执行此操作。用于此目的的选项是
SO_KEEPALIVE< /code>
,但这没什么用。
实现应用程序级心跳,并在指定时间内未收到数据或心跳时将套接字标记为断开连接(这几乎假设非阻塞模式,或者至少是
select(2)
循环超时)。There's no sane way of doing this on the TCP-only level. The option that was intended for this is
SO_KEEPALIVE
, but it's pretty useless.Implement application-level heartbeats and mark the socket disconnected when no data or heartbeat was received within specified time (this pretty much assumes non-blocking mode, or at least a
select(2)
loop with timeout).它与 C++ 代码无关。它与您正在使用的特定网络协议有关。假设是TCP,它基本上是永远的。空闲 TCP 连接没有与之关联的数据包,并且可能永远不会检测到连接丢失。
如果您想检测特定接口的丢失,请使用您平台的接口状态监控功能。
It has nothing to do with C++ code. It has everything to do with the particular network protocol you are using. Assuming it's TCP, it's basically forever. An idle TCP connection has no packets associated with it and may never detect a connection loss.
If you want to detect the loss of a particular interface, use your platform's interface state monitoring functions.