Java socket programming heartbeat package creation instance analysis
1. What is a heartbeat pack?
Heartbeat packet is a self-defined command word that regularly notifies the other party of its own status between the client and the server. It is sent at a certain time interval. It is similar to heartbeat, so it is called heartbeat packet.
Used to judge each other (devices, processes or other network elements) are running normally. Simple communication packets are sent regularly. If no response is received from the other party within the specified time period, it is judged that the other party is offline. It is used to detect the abnormal disconnection of TCP. The basic reason is that the server cannot effectively judge whether the client is online, that is, the server cannot distinguish whether the client is idle for a long time , or has dropped the line. The so-called heartbeat packet is that the client regularly sends simple information to the server to tell it I'm still there. The code is to send a fixed message to the server every few minutes. The server will reply to a fixed message after receiving it. If the server does not receive the client message within a few minutes, it will be deemed that the client is disconnected.
For example, some communication software is not used for a long time. If you want to know whether its status is online or offline, you need to heartbeat packets and send and receive packets regularly. Employer: it can be a customer or a server. It depends on which side is convenient and reasonable. It is generally a client. The server can also send heartbeat regularly. Generally speaking, for efficiency reasons, the client actively contracts to the server, rather than the server sends to the client. The client sends a packet every other period of time. For TCP, send, UDP and SendTo. After receiving it, the server will know that the current client is still in the "alive" state. Otherwise, if such a packet is not received after a certain period of time, the server considers that the client has been disconnected and carries out the corresponding client disconnection logic processing.
2. The following is a simple example of implementing Java heartbeat package
a) Server side server java
b) Client client java
3. Entity class java
summary
The above is all about the analysis of the creation instance of java socket programming heartbeat package in this paper. I hope it will be helpful to you. Interested friends can continue to refer to this website:
Java multithreading programming socket communication example code
Java programming using socket multithreading to access server file code example