在windows中的SO_KEEPALIVE时间的设置

在做服务器时,如果发生客户端网线或断电等非正常断开的现象,如果服务器没有设置SO_KEEPALIVE选项,则会一直不关闭SOCKET。

如果设置了SO_KEEPALIVE选项,则会使用操作系统的默认参数进行探测报文的发送。我们参考下MSN的一段话:

 http://msdn.microsoft.com/en-us/library/windows/desktop/dd877220(v=vs.85).aspx

SIO_KEEPALIVE_VALS (opcode setting: I, T==3)

Enables or disables the per-connection setting of the TCP keep-alive option which specifies the TCP keep-alive timeout and interval. For more information on the keep-alive option, see section 4.2.3.6 on the Requirements for Internet Hosts—Communication Layers specified in RFC 1122 available at the IETF website. The argument structure for SIO_KEEPALIVE_VALS is specified in the tcp_keepalive structure defined in the Mstcpip.h header file. This structure is defined as follows:

 

/* Argument structure for SIO_KEEPALIVE_VALS */
struct tcp_keepalive {
    u_long  onoff;
    u_long  keepalivetime;
    u_long  keepaliveinterval;
};

The value specified in the onoff member determines if TCP keep-alive is enabled or disabled. If the onoff member is set to a non-zero value, TCP keep-alive is enabled and the other members in the structure are used. The keepalivetime member specifies the timeout, in milliseconds, with no activity until the first keep-alive packet is sent. The keepaliveinterval member specifies the interval, in milliseconds, between when successive keep-alive packets are sent if no acknowledgement is received.

The SO_KEEPALIVE option, which is one of the SOL_SOCKET Socket Options, can also be used to enable or disable the TCP keep-alive on a connection, as well as query the current state of this option. To query whether TCP keep-alive is enabled on a socket, the getsockopt function can be called with the SO_KEEPALIVE option. To enable or disable TCP keep-alive, the setsockopt function can be called with the SO_KEEPALIVE option. If TCP keep-alive is enabled with SO_KEEPALIVE, then the default TCP settings are used for keep-alive timeout and interval unless these values have been changed using SIO_KEEPALIVE_VALS. The default settings when a TCP socket is initialized sets the keep-alive timeout to 2 hours and the keep-alive interval to 1 second. The number of keep-alive probes cannot be changed and is set to 10.

SIO_KEEPALIVE_VALS is supported on Windows 2000 and later.

 

也就是在2H内,没有数据交互,会发送探测报文。

在我们的使用中两个小时有时会太长,便可以通过WSAIoctl函数来设置,示例:

struct tcp_keepalive keepin;

struct tcp_keepalive keepout;

 

keepin.keepaliveinterval=10000;//10s 每10S发送1包探测报文,发5次没有回应,就断开

keepin.keepalivetime=6000*10;//60s 超过60S没有数据,就发送探测包

keepin.onoff=1;

 

WSAIoctl

(

tcplistenfd,

SIO_KEEPALIVE_VALS,

&keepin,sizeof(keepin),

&keepout,sizeof(keepout),

&bytesnum,NULL,NULL

);

这样就更改了timeout时间,间隔时间。

在vxworks里的设置,在下一篇说吧。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值