struct TCP_KEEPALIVE {
unsigned long onoff;
unsigned long keepalivetime;
unsigned long keepaliveinterval;
} ;
#define SIO_KEEPALIVE_VALS _WSAIOW(IOC_VENDOR,4)
void set_keepalive (SOCKET s)
{
BOOL bKeepAlive = TRUE;
int nRet = ::setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char*)&bKeepAlive, sizeof(bKeepAlive));
if
(nRet == SOCKET_ERROR)
{
return
;
}
struct TCP_KEEPALIVE inKeepAlive = {0};
unsigned long ulInLen = sizeof(struct TCP_KEEPALIVE);
struct TCP_KEEPALIVE outKeepAlive = {0};
unsigned long ulOutLen = sizeof(struct TCP_KEEPALIVE);
unsigned long ulBytesReturn = 0;
int ret = 0;
inKeepAlive.onoff = 1;
inKeepAlive.keepaliveinterval = 5000;
inKeepAlive.keepalivetime = 5000;
ret = WSAIoctl((unsigned int)s,
SIO_KEEPALIVE_VALS,
(LPVOID)&inKeepAlive,
ulInLen,
(LPVOID)&outKeepAlive,
ulOutLen,
&ulBytesReturn,
NULL,
NULL);
if
(ret == SOCKET_ERROR)
{
printf (
"error: %d\n"
, WSAGetLastError());
}
}