java长连接的实现方式,附上代码实现
时间: 2024-05-11 19:18:05 浏览: 83
Java长连接的实现方式有很多种,以下是其中一种基于Socket的实现方式:
服务端代码实现:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
// 创建ServerSocket对象,绑定监听端口
serverSocket = new ServerSocket(8888);
System.out.println("服务器已启动,等待客户端连接...");
while (true) {
// 等待客户端连接,accept()方法会阻塞当前线程
Socket socket = serverSocket.accept();
System.out.println("客户端连接成功!" + socket.getInetAddress() + ":" + socket.getPort());
// 开启线程处理客户端请求
new Thread(new Handler(socket)).start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭ServerSocket对象
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 处理客户端请求的线程
static class Handler implements Runnable {
private Socket socket;
public Handler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
BufferedReader reader = null;
PrintWriter writer = null;
try {
// 获取输入流
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// 获取输出流
writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
String message;
while ((message = reader.readLine()) != null) {
// 处理客户端请求
System.out.println("收到客户端消息:" + message);
writer.println("服务器已收到消息:" + message);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭流和Socket对象
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (writer != null) {
writer.close();
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
```
客户端代码实现:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
Socket socket = null;
BufferedReader reader = null;
PrintWriter writer = null;
try {
// 创建Socket对象,指定服务器地址和端口
socket = new Socket("127.0.0.1", 8888);
System.out.println("连接服务器成功!");
// 获取输入流
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// 获取输出流
writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入消息:");
String message = scanner.nextLine();
// 发送消息给服务器
writer.println(message);
// 接收服务器响应
String response = reader.readLine();
System.out.println("收到服务器消息:" + response);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭流和Socket对象
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (writer != null) {
writer.close();
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
```
以上代码实现了一个简单的长连接示例,当客户端连接到服务端后,可以发送消息给服务端并接收服务端的响应。服务端会开启一个新的线程处理客户端请求,以实现多客户端同时连接的效果。
阅读全文