基于java的客户端服务端的(心跳包,客户端断线重连,服务端超时断开客户端连接)
时间: 2023-07-13 10:20:46 浏览: 98
好的,这是一个基于Java的客户端服务端的心跳包、客户端断线重连和服务端超时断开客户端连接的示例:
服务器端代码:
```java
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static final int TIMEOUT = 5000; // 5秒超时
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Server started, waiting for client...");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Client connected.");
socket.setSoTimeout(TIMEOUT); // 设置超时时间
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
byte[] buffer = new byte[1024];
int len;
while (true) {
try {
len = inputStream.read(buffer);
if (len > 0) {
String message = new String(buffer, 0, len);
System.out.println("Received message from client: " + message);
outputStream.write(message.getBytes());
}
} catch (IOException e) {
System.out.println("Client disconnected.");
socket.close();
break;
}
}
}
}
}
```
客户端代码:
```java
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.util.Scanner;
public class Client {
private static final int HEARTBEAT_INTERVAL = 2000; // 2秒心跳包发送间隔
private static final int RECONNECT_INTERVAL = 5000; // 5秒重连间隔
private static final int TIMEOUT = 10000; // 10秒超时
public static void main(String[] args) throws IOException {
String serverHost = "localhost";
int serverPort = 8888;
Socket socket = null;
InputStream inputStream = null;
OutputStream outputStream = null;
Scanner scanner = new Scanner(System.in);
while (true) {
try {
socket = new Socket(serverHost, serverPort);
System.out.println("Connected to server.");
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
socket.setSoTimeout(TIMEOUT); // 设置超时时间
// 发送心跳包
Thread heartbeatThread = new Thread(() -> {
try {
while (true) {
outputStream.write("Heartbeat".getBytes());
Thread.sleep(HEARTBEAT_INTERVAL);
}
} catch (Exception e) {
// 心跳包发送失败,说明连接已断开,退出线程
}
});
heartbeatThread.start();
// 接收消息
byte[] buffer = new byte[1024];
int len;
while (true) {
try {
len = inputStream.read(buffer);
if (len > 0) {
String message = new String(buffer, 0, len);
System.out.println("Received message from server: " + message);
}
} catch (SocketException e) {
// 超时或连接断开,退出循环
System.out.println("Server disconnected.");
break;
}
}
// 关闭心跳包线程
heartbeatThread.interrupt();
try {
heartbeatThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
// 关闭连接和流
socket.close();
inputStream.close();
outputStream.close();
// 等待一段时间后重连
Thread.sleep(RECONNECT_INTERVAL);
} catch (IOException e) {
// 连接失败,等待一段时间后重连
System.out.println("Failed to connect to server.");
try {
Thread.sleep(RECONNECT_INTERVAL);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
}
```
这个例子实现了心跳包发送、客户端断线重连和服务端超时断开客户端连接的功能。客户端会在连接断开后自动重连,服务端会在客户端超时或断开连接后关闭连接。你可以根据自己的需求进行修改。
阅读全文