java实现socket长连接和短连接服务端、客户端代码实现及详解
时间: 2023-08-06 19:08:25 浏览: 179
好的,下面是Java实现socket长连接和短连接服务端、客户端的代码实现及详解:
## 长连接
### 服务端代码实现
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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 = new ServerSocket(8888);
System.out.println("服务端已启动,等待客户端连接...");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("客户端" + socket.getInetAddress().getHostAddress() + "已连接");
new Thread(new ServerThread(socket)).start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
static class ServerThread implements Runnable {
private Socket socket;
public ServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
while (true) {
String message = br.readLine();
if (message == null) {
break;
}
System.out.println("客户端" + socket.getInetAddress().getHostAddress() + ": " + message);
pw.println("服务端已收到消息:" + message);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
```
在服务端中,我们首先创建一个ServerSocket对象,指定端口号为8888,然后通过accept()方法来等待客户端的连接请求。当客户端连接成功后,我们创建一个新的线程来处理客户端的请求。在线程中,我们通过BufferedReader和PrintWriter来进行数据的读写操作。当客户端发送消息后,服务端会进行回复,直到收到客户端的断开连接请求。
### 客户端代码实现
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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 br = null;
PrintWriter pw = null;
Scanner scanner = new Scanner(System.in);
try {
socket = new Socket("127.0.0.1", 8888);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream(), true);
while (true) {
String message = scanner.nextLine();
pw.println(message);
String response = br.readLine();
System.out.println("服务端回复:" + response);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (pw != null) {
pw.close();
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
```
在客户端中,我们首先创建一个Socket对象,指定IP地址和端口号为服务端的IP地址和端口号,然后通过BufferedReader和PrintWriter来进行数据的读写操作。当客户端发送消息后,服务端会进行回复,直到客户端输入exit后,客户端会发送断开连接请求,服务端会关闭连接。
## 短连接
### 服务端代码实现
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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 = new ServerSocket(8888);
System.out.println("服务端已启动,等待客户端连接...");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("客户端" + socket.getInetAddress().getHostAddress() + "已连接");
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
String message = br.readLine();
System.out.println("客户端" + socket.getInetAddress().getHostAddress() + ": " + message);
pw.println("服务端已收到消息:" + message);
pw.close();
br.close();
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
```
在服务端中,我们依然是创建一个ServerSocket对象,指定端口号为8888,然后通过accept()方法来等待客户端的连接请求。当客户端连接成功后,服务端会通过BufferedReader来读取客户端发送的消息,然后通过PrintWriter来回复客户端。由于是短连接,所以服务端会在处理完客户端请求后立即关闭连接。
### 客户端代码实现
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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 br = null;
PrintWriter pw = null;
Scanner scanner = new Scanner(System.in);
try {
socket = new Socket("127.0.0.1", 8888);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream(), true);
System.out.println("请输入消息:");
String message = scanner.nextLine();
pw.println(message);
String response = br.readLine();
System.out.println("服务端回复:" + response);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (pw != null) {
pw.close();
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
```
在客户端中,我们首先创建一个Socket对象,指定IP地址和端口号为服务端的IP地址和端口号,然后通过BufferedReader和PrintWriter来进行数据的读写操作。当客户端发送消息后,服务端会进行回复,客户端会读取服务端的回复,然后关闭连接。
阅读全文