本文介绍了如何设置winsock的保持活动间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 winsock 和 TCP.我已将 KeepAlive 选项设置如下

I am using winsock and TCP.I have set the KeepAlive option as follows

int aliveToggle = 1;
setsockopt(mySocket,SOL_SOCKET,SO_KEEPALIVE,(char*)&aliveToggle, sizeof(aliveToggle));

但是如何指定Keep aLive的时间和间隔?

But how to specify the Keep aLive time and interval?

我使用的是在 Windows 7 上运行的 VC++.

I am using VC++ running on windows 7.

推荐答案

键 \HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Tcpip\Parameters 下的两个 per-interface 注册表设置控制 TCP/IP keep-alives 的行为:

Two per-interface registry settings under the key \HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Tcpip\Parameters control the behavior of TCP/IP keep-alives:

KeepAliveTime 值指定 TCP 连接在没有流量的情况下处于空闲状态的时间,然后 TCP 发送保持活动数据包.默认值为 7,200,000 毫秒 (ms) 或 2 小时.

The KeepAliveTime value specifies how long the TCP connection sits idle, with no traffic, before TCP sends a keep-alive packet. The default is 7,200,000 milliseconds (ms) or 2 hours.

KeepAliveInterval 值指示在发送保持连接之后在重复保持连接之前等待响应的毫秒数.如果没有收到响应,TCP/IP 堆栈将继续在此时间间隔发送保持活动,直到收到响应或直到堆栈达到 TCPMaxDataRetransmissions 注册表项中指定的数据包重试限制.KeepAliveInterval 默认为 1 秒 (1000 .

The KeepAliveInterval value indicates how many milliseconds to wait for a response after sending a keep-alive before repeating the keep-alive. If no response is received, the TCP/IP stack continues sending keep-alives at this interval until a response is received or until the stack reaches the packet retry limit specified in the TCPMaxDataRetransmissions registry key. KeepAliveInterval defaults to 1 second (1000 .

默认情况下禁用 TCP keep-alives,但 Windows Sockets 应用程序可以使用 setsockopt 函数在每个连接的基础上启用它们.

TCP keep-alives are disabled by default, but Windows Sockets applications can use the setsockopt function to enable them on a per-connection basis.

注意 如果开发人员选择在特定连接上使用 TCP 保持活动消息,则这些消息的时间由上述注册表值指定.不可能对不同的保持活动请求使用不同的时间.

Note  If the developer elects to use TCP keep-alive messages on a particular connection, the timing of those messages is specified by the registry values described preceding. It is not possible to use different timing on different keep-alive requests.

这篇关于如何设置winsock的保持活动间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 20:55