写一个JAVA代码,聊天程序,有服务器端和客户端
时间: 2024-03-21 21:37:37 浏览: 28
以下是一个基本的Java聊天程序,包括服务器端和客户端代码。请注意,这只是一个简单的示例,需要根据实际需求进行修改和完善。
服务器端代码:
```java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server {
private static ArrayList<ClientHandler> clients = new ArrayList<ClientHandler>();
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(1234); // 监听端口号1234
System.out.println("服务器已启动,等待连接...");
while (true) {
Socket clientSocket = serverSocket.accept(); // 等待客户端连接
System.out.println("客户端已连接:" + clientSocket.getInetAddress().getHostAddress());
// 创建新的ClientHandler线程
ClientHandler clientHandler = new ClientHandler(clientSocket, clients);
clients.add(clientHandler);
Thread thread = new Thread(clientHandler);
thread.start();
}
}
}
```
客户端代码:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 1234); // 连接到服务器
System.out.println("连接服务器成功!");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = reader.readLine()) != null) {
out.println(line);
out.flush();
System.out.println("发送消息:" + line);
String response = in.readLine();
System.out.println("收到消息:" + response);
}
in.close();
out.close();
socket.close();
}
}
```
ClientHandler类:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
public class ClientHandler implements Runnable {
private Socket clientSocket;
private ArrayList<ClientHandler> clients;
private BufferedReader in;
private PrintWriter out;
public ClientHandler(Socket socket, ArrayList<ClientHandler> clients) throws IOException {
this.clientSocket = socket;
this.clients = clients;
this.in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
this.out = new PrintWriter(clientSocket.getOutputStream());
}
@Override
public void run() {
try {
while (true) {
String line = in.readLine();
System.out.println("收到消息:" + line);
// 广播消息给其他客户端
for (ClientHandler client : clients) {
if (client != this) {
client.out.println(line);
client.out.flush();
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
clientSocket.close();
clients.remove(this);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
这个聊天程序使用了Socket编程实现,服务器端通过ServerSocket监听客户端连接,每当有一个客户端连接时,就创建一个新的ClientHandler线程来处理该客户端的消息。客户端通过Socket连接服务器,收发消息时需要使用BufferedReader和PrintWriter等输入输出流。ClientHandler类用来处理客户端的消息,收到消息后会广播给其他客户端。该程序只是一个简单的示例,需要根据实际需求进行修改和完善。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)