如何判断一个socket连接是否关闭?
如何判断一个socket连接是否关闭?
[解决办法]
发心跳包
www.google.com
解决了别忘了告诉我………
[解决办法]
struct tcp_keepalive inKeepAlive, outKeepAlive;
unsigned long ulInLen, ulOutLen, ulBytesReturn;
inKeepAlive.onoff = 1;
inKeepAlive.keepaliveinterval = 30000;
inKeepAlive.keepalivetime = 300000;
ulInLen = sizeof(tcp_keepalive);
ulOutLen = sizeof(tcp_keepalive);
ulBytesReturn = 0;
memset( &outKeepAlive, 0, sizeof(tcp_keepalive) );
if (WSAIoctl(sock, SIO_KEEPALIVE_VALS, (LPVOID)&inKeepAlive, ulInLen,
(LPVOID)&outKeepAlive, ulOutLen, &ulBytesReturn, NULL, NULL) == SOCKET_ERROR){writelog( "WSAIoctl(keep alive) failed. error code = " + WSAGetLastError()) ;
}
如果 socket 关闭你就会接到通知。
p.s. 我的程序是 IOCP 的,通知从 GetQueuedCompletionStatus() 返回中得到。估计要是用 select() 的话,也可以从 errorlist 中得到;