Java Socket Network programming heartbeat design Concept

Source: Internet
Author: User

Java Socket Network programming heartbeat design Concept

1, generally used to determine the other side (equipment, process or other network elements) is normal movement, the general use of timing to send a simple communication packet, if not received in the specified time period of response, the other side has been taken off. Used to detect an abnormal disconnection of TCP. Generally used to determine the other side (equipment, process or other network elements) is normal movement, generally using a simple communication packet timing, if not received in the specified time period of response, the other side has been taken off. Used to detect an abnormal disconnection of TCP. The basic reason is that the server can not effectively determine whether the client is online that is, the server can not distinguish between the client is a long time in idle, or has dropped the situation. The so-called heartbeat packet is the client sends a simple message to the server and tells it I'm still there. The code is to send a fixed message every few minutes to the server, the server receives a fixed message if the server does not receive the client information within a few minutes, depending on the client disconnect. For example, some communication software is not used for a long time, want to know whether its status is online or offline need heartbeat packet, scheduled packet delivery. Contracting Party: Can be a customer can also be a service side, see which way to achieve convenient and reasonable. Typically a client. The server can also periodically poll for a heartbeat. In general, for efficiency reasons, the client is actively contracting to the server.

2, is in the client and the server time to notify each other's own state of a self-defined command word, according to a certain interval sent, similar to the heartbeat, so called Heartbeat packet.

3, is timed to send to the other party a packet, tell the other person is still maintaining the dialogue, while obtaining the data returned, to determine whether the other party in the session.

4, the client sends a packet at intervals, using TCP, send, using UDP, with SendTo, the server received, know that the current client is still in the "Alive" state, otherwise, if not received at a certain time such a package, the server thinks the client has been disconnected, The corresponding client-side disconnect logic is processed.

First, what is heartbeat detection

Determine whether the other side (equipment, process or other network elements) is normal movement, the general use of timing to send a simple communication packet, if not received in the specified time period of response, the other side has been taken off. Used to detect an abnormal disconnection of TCP.

The basic reason is that the server can not effectively determine whether the client is online that is, the server can not distinguish between the client is a long time in idle, or has dropped the situation. The so-called heartbeat packet is the client sends a simple message to the server and tells it I'm still there.

The code is to send a fixed message every few minutes to the server, and the server responds with a fixed message after it is received. If the server does not receive client information within a few minutes, the client is considered disconnected. For example, some communication software is not used for a long time, want to know whether its status is online or offline need heartbeat packet, scheduled packet delivery.

The employer can be the customer or the service side, to see which way to achieve convenient and reasonable. typically a client. The server can also periodically poll for a heartbeat.

In general, for efficiency reasons, the client is actively contracting to the server, not the other way around.

Second, about the return value of the Send function

We know that when the client disconnects the TCP connection gracefully, the server uses the Send function to receive a return value of 0 (indicating that the TCP connection is broken). However, the successful return of the Send function can only indicate that the sending data has entered the send queue of the socket core, which is not necessarily online or has been successfully received. This may be because send only copies data to the send buffer, and the buffer is not full at the beginning, so there will be no error, only after a long period of time, the return value of send will be-1.

Third, timed ping

If the server does not respond to heartbeat detection. You can ping a server periodically to detect whether a TCP connection is broken, such as Sina (a server that must allow pings). Ping is sending an ICMP packet to the remote host, which is considered a timeout if the response is not received within a given time, and if the response is received, the ICMP packets received are analyzed, and the TTL, type, and time are obtained.

However, the ping packet has 32 bytes, and if it is a wireless connection, it is sometimes necessary to consider traffic problems.

Iv. using the TCP survival timer

Java Socket Network programming heartbeat design Concept

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.