//the realization of the heartbeat packets - open KeepAlive
/* BOOL bKeepAlive=TRUE;
Int nRet=: : setsockopt (sockClient, SOL_SOCKET, SO_KEEPALIVE, (char *) & amp; BKeepAlive, sizeof (bKeepAlive));
If (nRet!=0)
{
AfxMessageBox (_T (" error "));
return 0;
}
*///change the socket keep alive heartbeat package for 10 s, and send 3 times -- -- -- -- --
Settings KeepAlive parametersTcp_keepalive inKeepAlive={0};//input parameter
Tcp_keepalive outKeepAlive={0};//output parameter
Unsigned long ulBytesReturn=0;
InKeepAlive. Onoff=true;
InKeepAlive. Keepaliveinterval=400;//the time interval between two KeepAlive probe
InKeepAlive. Keepalivetime=100;//before the first KeepAlive exploration TCP free time
Int nRet=WSAIoctl (sockClient SIO_KEEPALIVE_VALS, & amp; InKeepAlive, sizeof (inKeepAlive),
& OutKeepAlive, sizeof (outKeepAlive), & amp; UlBytesReturn, NULL, NULL);
If (SOCKET_ERROR==nRet)
{
AfxMessageBox (_T (" error "));
return false;
}
CodePudding user response:
SO_KEEPALIVE is an internal implementation TCP heartbeat, I generally set to 10 seconds to detect a heartbeat,Detect disconnect, depend on recv. Returns a negative number or zero is disconnected
CodePudding user response:
Or oneself do a heartbeat thread on pointCodePudding user response:
Choose FD_CLOSE WSAEventSelect, monitor the closing eventCodePudding user response:
You can create a connection to maintain heartbeat mechanism alone ~CodePudding user response: