The heartbeat packet is sent, and there are usually two kinds of techniques
Method 1: The application layer implements its own heartbeat package
The application sends the heartbeat packet to detect the connection is normal, the approximate method is: The server in a timer event to send a short and short packet to the client, and then start a low-level thread, in which the client's response is constantly detected, if not received a response from the client for a certain period of time, That is, the client is considered to be out of line; Similarly, if the client does not receive a heartbeat packet from the server for a certain amount of time, the connection is considered unavailable.
2:tcp keepalive mechanism of method keepalive
Because one server is usually connected to multiple clients, it is much simpler and more complex to implement the heartbeat package by the user at the application layer, and the TCP/IP protocol layer for the built-in keepalive function is more simple. Whether it is a server or a client, when one side turns on the KeepAlive function, it will automatically send the heartbeat packet to the other party within the specified time, and the other side will automatically reply after receiving the heartbeat packet to tell the other side I am still online. Because the KeepAlive feature requires additional bandwidth and traffic, the TCP protocol layer does not turn on the KeepAlive feature by default, although this is trivial, it increases the cost in a metered environment, and on the other hand, the keepalive setting may be A healthy TCP connection is disconnected because of transient network fluctuations. Also, the default keepalive timeout requires 7,200,000 MilliSeconds, which is 2 hours, and the number of probes is 5 times. For many server-side applications, 2 hours of idle time is too long. Therefore, we need to manually turn on the KeepAlive function and set reasonable keepalive parameters.
heartbeat packet mechanism Skipping packets is called the heartbeat package because: it is like a heartbeat, every fixed time to send to tell the server, the client is still alive. In fact, this is to maintain a long connection, as for the contents of this package, there is no special provisions, but generally is a small package, or only a packet of empty packets in Baotou.
in the TCP mechanism, itself is the existence of a heartbeat packet mechanism, that is, TCP option: So_keepalive. The system defaults to a 2-hour heartbeat rate set. But it does not check the machine power, cable pull out, firewall these disconnection. and the logic layer processing disconnection may not be so good processing. In general, if it is only used to survive or can. The
heartbeat package is generally implemented by sending an empty echo packet at the logical level. The next timer, at a certain time interval to send an empty packet to the client, and then the client feedback a same empty package back, the server if within a certain period of time to receive the client sent over the feedback package, it is only recognized that the line has been dropped.
In fact, to determine the drop line, only need to send or recv, if the node (firewall) automatically disconnects a connection that has no data interaction for a certain amount of time. At this time, we need our heartbeat package, which is used to maintain long connections and keep alive.
After the disconnection is learned, the server logic may need to do something, such as data cleanup after disconnection, reconnect ... Of course, this nature is to be done by the logic layer on demand.
Overall,
5 If the client's timeout timer expires and the reply packet is still not received, the server hangs
Transferred from: http://blog.sina.com.cn/s/blog_a459dcf5010153m5.html
According to the above, we can know that when the end is disconnected in a non-graceful way, we can set the So_keepalive property so that we find out whether the other's TCP connection is still there after 2 hours.
Specific operation:
Set KeepAlive
1, BOOL bkeepalive = TRUE;
int nret=::SetSockOpt (Sockclient,sol_socket,so_keepalive, (char*) &bkeepalive,sizeof (bkeepalive));
if (nret!=0)
{
AfxMessageBox ("error");
return;
}
2, feel two hours time too long can set the method 1
Set KeepAlive detection time and frequency
Tcp_keepalive inkeepalive = {0}; Input parameters
unsigned long Ulinlen = sizeof (tcp_keepalive);
Tcp_keepalive outkeepalive = {0}; Output parameters
unsigned long Uloutlen = sizeof (tcp_keepalive);
unsigned long ulbytesreturn = 0;
Set the socket's keep alive to 10 seconds and send the number 3 times
Inkeepalive.onoff = 1;
Inkeepalive.keepaliveinterval = 4000; Two time intervals between keepalive probes
Inkeepalive.keepalivetime = 1000; TCP null-closed time before first keepalive detection started
nret=WSAIoctl (Sockclient,
Sio_keepalive_vals,
(LPVOID) &inkeepalive,
Ulinlen,
(LPVOID) &outkeepalive,
Uloutlen,
&ulbytesreturn,
Null
NULL);
if (Socket_error = = nret)
{
AfxMessageBox ("error");
Return
}
3, feel two hours time is too long can be set by oneself method2
So we can get
int keepidle = 6;
int keepinterval = 5;
int keepcount = 3;
Setsockopt (LISTENFD, Sol_tcp, Tcp_keepidle, (void *) &keepidle, sizeof (Keepidle));
Setsockopt (LISTENFD, SOL_TCP,TCP_KEEPINTVL, (void *) &keepinterval, sizeof (Keepinterval));
Setsockopt (Listenfd,sol_tcp, tcp_keepcnt, (void *) &keepcount, sizeof (Keepcount));
See: http://blog.csdn.net/gavin1203/article/details/5290609
for the operation of setsockopt , see: http://www.cnblogs.com/hateislove214/archive/2010/11/05/1869886.html
Mechanism principle of heartbeat packet