第一步启动服务器
package work12.chats;
import java.io.*;
import java.net.*;
import java.util.*;
public class MultiUserChatServer {
private static final int PORT = 8888;
//保存所有连接到服务器的客户端
private static final List
public static void main(String[] args) {
System.out.println("启动服务端口: " + PORT);
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
while (true) {
Socket socket = serverSocket.accept();
new Thread(new ClientHandler(socket)).start(); // 为每个客户端创建一个线程
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
// 处理客户端连接的线程
private static class ClientHandler implements Runnable {
private String name;
private BufferedReader reader;
private PrintWriter writer;
public ClientHandler(Socket socket) {
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void run() {
try {
name = reader.readLine(); // 读取客户端发送的用户名
System.out.println(name + " 加入聊天室");
// 向所有客户端发送欢迎消息
for (PrintWriter w : writers) {
w.println(name + " 加入聊天室");
}
writers.add(writer); // 将该客户端的 PrintWriter 添加到列表中
String message;
while ((message = reader.readLine()) != null) {
// 向所有客户端转发消息
for (PrintWriter w : writers) {
w.println(name + ": " + message);
}
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
writers.remove(writer); // 将该客户端的 PrintWriter 从列表中移除
try {
reader.close();
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println(name + " 离开聊天室");
// 向所有客户端发送离开消息
for (PrintWriter w : writers) {
w.println(name + " 离开聊天室");
}
}
}
}
}
第二步
package work12.chats;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class MultiUserChatClient extends JFrame {
private static final String SERVER_HOST = "localhost";
private static final int SERVER_PORT = 8888;
private Socket socket;
private BufferedReader reader;
private PrintWriter writer;
private JTextArea chatTextArea;
private JTextField messageTextField;
public MultiUserChatClient(String username) {
super("聊天室 - " + username);
// 创建界面元素
chatTextArea = new JTextArea();
chatTextArea.setEditable(false);
JScrollPane chatScrollPane = new JScrollPane(chatTextArea);
messageTextField = new JTextField();
messageTextField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
JButton sendButton = new JButton("发送");
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
JPanel inputPanel = new JPanel(new BorderLayout());
inputPanel.add(messageTextField, BorderLayout.CENTER);
inputPanel.add(sendButton, BorderLayout.EAST);
// 将界面元素添加到窗口中
setLayout(new BorderLayout());
add(chatScrollPane, BorderLayout.CENTER);
add(inputPanel, BorderLayout.SOUTH);
// 设置窗口属性
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setVisible(true);
// 连接服务器并处理消息
try {
socket = new Socket(SERVER_HOST, SERVER_PORT);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);
//第一条消息,告诉服务器客户端的用户名
writer.println(username); // 发送用户名到服务器
new Thread(new IncomingReader()).start(); // 启动接收消息的线程
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void sendMessage() {
String message = messageTextField.getText();
writer.println(message); // 发送消息到服务器
messageTextField.setText("");
}
private class IncomingReader implements Runnable {
public void run() {
try {
String message;
while ((message = reader.readLine()) != null) {
chatTextArea.append(message + "\n"); // 显示接收到的消息
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
String username = JOptionPane.showInputDialog("请输入用户名");
if (username != null && !username.isEmpty()) {
new MultiUserChatClient(username);
}
}
}