tcp long connection, heartbeat packet

Long connection

After TCP three-way handshake to establish a connection, long connection means to send data packets with or without long-term connections are established;
when long connections also naturally have short connections, short connection means that both sides have data transmission connection is established, send a few times after the request, active or passive disconnected.

Heartbeat

One way is to detect a heartbeat system is alive or a network link is smooth, the practice is regularly sent heartbeat packet is detected to the system, the detection system heartbeat packet is received reply, reply received instructions other survival. Heartbeat able to provide long keep-alive connection capabilities to detect long connection is working, once the link is dead, it is not available, and be able to know as soon as possible, and then do other highly available measures to ensure the normal operation of the system.

Long advantages connected

  • Reducing connection time-consuming process of establishing the
    TCP connection establishment requires three-way handshake, three-way handshake is to say three times need to interact in order to establish a connection channel, between the city-level machine is probably ms delay, the impact is not big, if it is Beijing and Shanghai room, go green one to one back takes about 30ms, if long connection, this optimization is still very substantial.

  • Facilitate the achievement data push
    data interaction - push mode implementation is provided a network connection length, with long, connecting both ends of the data easily push each other to interact.

Long connection keep alive

Operating system implementation:
TCP The KeepAlive mechanism (this mechanism is not the content of the TCP protocol specification, to be implemented by the operating system) is turned on after the KeepAlive mechanism, (typically time 7200s, tcp_keepalive_time parameter) is not on the link data in a certain time in the case of transmission, TCP layer sends a corresponding KeepAlive as probes to determine the availability of connection, after detecting failure retry 10 (tcp_keepalive_probes parameter) times, each time interval of 75s (tcp_keepalive_intvl parameter), all the probe fails, only that the current connection It has been unavailable. These parameters are machine-level, you can be adjusted. KeepAlive keep-alive mechanism will play a role only in case the link is idle.

A reliable system, keep alive long connection certainly depends heartbeat to ensure the application layer.

Application layer:
If the client has disappeared and the connection is not disconnected, so that will keep a half-open connections on the server, and the server and is waiting for data from the client, then the server will always wait for the client's data. Keepalive function is to try to detect the server end of this half-open connection.

If a given connection without any action within two hours, the server sends to the client a packet detecting section, in response to detecting four client state based on the client host:

  • Client host is still running, and the server is reachable. At this time, the TCP client response, the server will keep alive timer is reset.
  • Client host has crashed and is being shut down or restart. TCP client can respond under the above circumstances. The server will not receive a response to the detection of the client. A total of 10 transmission server such detection, each spacer 75 seconds. If the server does not receive any response, it assumes that the client is closed and terminates the connection.
  • Client crashed and has been restarted. The server receives a response to its keepalive This response is reset, so that the server terminates the connection.
  • Client running, but the server is unreachable. This situation is similar to the second state.

Heartbeat use

A program
simplest strategy is of course the timing n second client send heartbeat packets, the server receives the heartbeat packet, respond to the client's heart, if the client does not receive a heartbeat m consecutive second package, the active disconnect and re even the normal on the service request sent by the server temporarily.

Option II
thus transferred some invalid packets a little more, you can do some optimization. Because the heart is a kind of a probe request, a normal request service in addition to doing the service processing, the detection function can also be used, such as case there is a request to be sent to the server, the request can be regarded as one heartbeat, the server receipt of the request, return after treatment, as long as the server has a reply, it indicates that the link is on, if the client requests more free time, the server has no data to reply, on the use of heartbeat detection, so that the effective use of normal the request as a heartbeat function, reduce the ineffective data transmission.

发布了8 篇原创文章 · 获赞 18 · 访问量 9445

Guess you like

Origin blog.csdn.net/weixin_38054045/article/details/104257511