1.What is a heartbeat bag?
A heartbeat packet is a self-defined command word that regularly notifies the other party of its own status between the client and the server, and is sent at a certain time interval, similar to a heartbeat, so it is called a heartbeat packet.
Used to determine whether the other party (device, process or other network element) is operating normally. It uses timed to send simple communication packets. If no response from the other party is received within a specified time period, it is determined that the other party is offline. Used to detect abnormal disconnection of TCP. The basic reason is that the server cannot effectively determine whether the client is online, that is, the server cannot distinguish whether the client is idle for a long time or has been disconnected. The so-called heartbeat packet is that the client sends simple information regularly to the server to tell it that I am still there. The code is to send a fixed message to the server every few minutes. After receiving it, the server will reply to a fixed message. If the server does not receive the client information within a few minutes, the client will be 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 a heartbeat packet and send and receive packets regularly. The contractor: It can be a customer or a server. It depends on which side is convenient and reasonable to implement, and it is generally a client. The server can also cause heartbeats to occur regularly. Generally speaking, for efficiency reasons, the client actively sends packets to the server, rather than the server sends them to the client. The client sends a packet every once in a while, using TCP, sends it with send, using UDP, sends it with sendto. After the server receives it, it will know that the current client is still in a "alive" state. Otherwise, if such a packet is not received after a certain period of time, the server believes that the client has been disconnected and performs the corresponding client disconnection logic processing.
2. The following is a simple example of implementing the Java heartbeat package
a) Server side Server.java
package cn.yw.socket.heart;import java.io.ObjectInput;import java.io.ObjectInputStream;import java.net.ServerSocket;import java.net.Socket;public class Server extends Thread{private ServerSocket server = null;Object obj = new Object();@Override public void run() {try{while(true){server = new ServerSocket(25535);Socket client = server.accept();synchronized(obj){new Thread(new Client(client)).start();}}}}catch(Exception e){e.printStackTrace();}}/** * Client thread* @author USER * */class Client implements Runnable{Socket client;public Client(Socket client){this.client = client;}@Override public void run() {try{while(true){ObjectInput in = new ObjectInputStream(client.getInputStream());Entity entity = (Entity)in.readObject();System.out.println(entity.getName());System.out.println(entity.getSex());}}catch(Exception e){e.printStackTrace();}}}/** *The entry main method of the program* @param args */public static void main(String[] args){new Server().start();}}}b) Client.java
package cn.yw.socket.heart;public class Client extends Thread{@Override public void run() {try{while(true){ClientSender.getInstance().send();synchronized(Client.class){// this.wait(5000);Thread.sleep(2000);}}}}catch(Exception e){e.printStackTrace();}}/** * Program entry main method* @param args */public static void main(String[] args){Client client = new Client();client.start();}} package cn.yw.socket.heart;import java.io.ObjectOutputStream;import java.net.InetAddress;import java.net.Socket;public class ClientSender{private ClientSender(){}Socket sender = null;private static ClientSender instance;public static ClientSender getInstance(){if(instance==null){synchronized(Client.class){instance = new ClientSender();}}return instance;}public void send(){try{sender = new Socket(InetAddress.getLocalHost(),25535); while(true){ObjectOutputStream out = new ObjectOutputStream(sender.getOutputStream());Entity obj = new Entity();obj.setName("syz");obj.setSex("Male");out.writeObject(obj);out.flush();Thread.sleep(5000);}}catch(Exception e){}}}3. Entity class Entity.java
package cn.yw.socket.heart;import java.io.Serializable;public class Entity implements Serializable{private String name;private String sex;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}}Summarize
The above is all the content of this article about the analysis of the Java Socket programming heartbeat package creation example, I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Sample code for implementing socket communication in Java multithreaded programming
Java programming code example using socket multithreading to access server files
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!