Home > Software engineering >  VS2013MFC based on TCP/IP communications of the realization of the function of the heart
VS2013MFC based on TCP/IP communications of the realization of the function of the heart

Time:09-17

I wrote a with VS2013MFC based on TCP/IP server and client, using a computer to control multiple computers to open the PPT, demo PPT on multiple projectors, perfect! But there is a regret, is not to achieve the heart function, I would like to use WSAIoctl to realize the heartbeat, procedure is as follows: compile, run all normal, but whether to separate the application on the server, alone on the client, or the client to the server, I deliberately withdrew from the server or client, such as more than 10 minutes, also does not have occurrence heartbeat, give advice or comments please!

//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 point

CodePudding user response:

Choose FD_CLOSE WSAEventSelect, monitor the closing event

CodePudding user response:

You can create a connection to maintain heartbeat mechanism alone ~

CodePudding user response:

reference 1st floor smwhotjay 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 to disconnect the

Thank you for your reply!
I set the above procedure, right?
OnSocket news I am used to receive each other's data, if the other party has been closed, this message does not respond, can only use a thread to do?
  • Related