MSDN中TCP keepalive时效的说明以及设置TCP Keepalive的代码

来源:互联网 发布:mac鼠标在windows系统 编辑:程序博客网 时间:2024/04/28 05:49
说明:
A TCP keep-alive packet is simply an ACK with the sequence number set to one less than the current sequence number for the connection. A host receiving one of these ACKs will respond with an ACK for the current sequence number.
翻译:
一个TCP keep-alive 包是一个简单的ACK,该ACK包内容为 一个比当前连接sequence number 小于一的包。主机接受到这些ACKs会返回一个包含当前sequence number 的ACK包。

说明:
Keep-alives can be used to verify that the computer at the remote end of a connection is still available.
翻译:
Keep-alives一般被用来验证远端连接是否有效。

说明:
TCP keep-alives can be sent once every KeepAliveTime (defaults to 7,200,000 milliseconds or 2 hours), if no other data or higher-level keep-alives have been carried over the TCP connection.
翻译:
如果该连接上没有其他数据被传输,或者更高level 的 keep-alives被传送,keep-alives 在每个KeepAliveTime被发送。(默认是 7,200,000 milliseconds ,也就是2个小时)。

说明:
If there is no response to a keep-alive, it is repeated once every KeepAliveInterval seconds. KeepAliveInterval defaults to 1 second. NetBT connections, such as those used by many parts of the Microsoft networking functionality, send NetBIOS keep-alives more frequently, so usually no TCP keep-alives will be sent on a NetBIOS connection.
原翻译:
如果对于keep-alive连接端没有回应,他继续将在每KeepAliveInterval秒发送,KeepAliveInterval默认为1秒,在NETBT连接,比如Microsoft networking的很多功能性的部分,发送NETBios keep-alives频率高,所以,在一个NetBios连接中,TCP keep-alives不被发送。  

此处感谢 网友of123 修正翻译(给大家造成的误解博主向所有的读者表示歉意):
引用

如果没有收到 keep-alive 应答,keep-alive 将在每 KeepAliveInterval 秒重发一次。KeepAliveInterval 默认为1秒。如 Microsoft 网络功能中很多部分中采用的 NETBT 连接,更常见的是发送 NETBios keep-alives,所以,在 NetBios 连接中通常不发送TCP keep-alives。


说明:
TCP keep-alives are disabled by default, but Windows Sockets applications can use the SetSockOpt function to enable them.
翻译:
TCP保持连接默认被禁用,但是微软Sockets应用程序可以使用SetSockOpt函数去启用他们。


几点比较重要的说明:

1:TCP 的默认时效是2个小时。
2:如果发送Keep-alive没有回应,将会在KeepAliveInterval再次发送,不过这里是否需要系统来进行判断,并断开连接?

设置keepalive代码如下:
view plaincopy to clipboardprint?
  1.                 tcp_keepalive live,liveout;   
  2.                 live.keepaliveinterval=500;    
  3.                 live.keepalivetime=3000; //勘误  1分钟是 60000 以此类推   
  4.                 live.onoff=TRUE;   
  5.                 int iRet = setsockopt(Socket,SOL_SOCKET,SO_KEEPALIVE,(char *)Opt,sizeof(int));   
  6.                 if(iRet == 0){   
  7.   
  8.                         DWORD dw;   
  9.                    
  10.                         if(WSAIoctl(Socket,SIO_KEEPALIVE_VALS,&live,sizeof(live),&liveout,sizeof(liveout),&dw,NULL,NULL)== SOCKET_ERROR){   
  11.   
  12.                                 //Delete Client   
  13.   
  14.                                 return;   
  15.                         }   
  16.                    }   


判断掉线并处理:
view plaincopy to clipboardprint?
  1. BOOL bReturn = GetQueuedCompletionStatus(   
  2.       g_hIOCompletionPort,   
  3.       &dwBytesTransfered,   
  4.       (LPDWORD)&lpContext,   
  5.       &pOverlapped,   
  6.       INFINITE);   
  7. if (bReturn== false){   
  8.    //Client has gone delete client and close socket   
  9. }