javapackage p1008; import java.io.*; import java.net.*; import java.util.*; public class ChatServer { private static final int PORT = 12345; private static Set<ClientHandler> clientHandlers = new HashSet<>(); public static void main(String[] args) { System.out.println("聊天服务器启动..."); try (ServerSocket serverSocket = new ServerSocket(PORT)) { while (true) { new ClientHandler(serverSocket.accept()).start(); } } catch (IOException e) { e.printStackTrace(); } } private static class ClientHandler extends Thread { private Socket socket; private PrintWriter out; private BufferedReader in; public ClientHandler(Socket socket) { this.socket = socket; } public void run() { try { in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); synchronized (clientHandlers) { clientHandlers.add(this); } // 发送欢迎消息 out.println("欢迎来到聊天室!"); System.out.println(socket.getLocalAddress()+" 来到了聊天室~!"); synchronized (clientHandlers) { for (ClientHandler client : clientHandlers) { if (client != this) { // 排除发送者 client.out.println(socket.getLocalAddress()+" 来到了聊天室~!"); } } } String message; while ((message = in.readLine()) != null) { System.out.println("收到消息: " + message); // 广播消息给所有在线用户,排除发送者 synchronized (clientHandlers) { for (ClientHandler client : clientHandlers) { if (client != this) { // 排除发送者 client.out.println(message); }else{ client.out.println("自己说:"+message); } } } if(message.equals("quit")){ System.out.println(socket.getLocalAddress()+" 退出服务器"); break; } } } catch (IOException e) { e.printStackTrace(); } finally { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } synchronized (clientHandlers) { clientHandlers.remove(this); } } } } }
javapackage p1008; import java.io.*; import java.net.*; public class ChatClient { private static final String SERVER_ADDRESS = "localhost"; // 服务器地址 private static final int PORT = 12345; public static void main(String[] args) { try (Socket socket = new Socket(SERVER_ADDRESS, PORT); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) { // 接收服务器消息的线程 new Thread(() -> { String response; try { while ((response = in.readLine()) != null) { System.out.println(response); } } catch (IOException e) { System.out.println("结束与服务器通讯~!"); //e.printStackTrace(); } }).start(); // 发送消息到服务器 String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); if(userInput.equals("quit")) break; } } catch (IOException e) { e.printStackTrace(); } System.out.println("结束"); } }
java 模拟多人聊天室,服务器与客户机
laocooon5238578862024-10-07 22:52
相关推荐
文丑颜不良啊25 分钟前
LeetCode3281~3300题目解析nice_lcj5201 小时前
【从零理解计算机网络 · 02】分层模型:别背 OSI 七层,看数据怎么被“打包“和“拆包“ShineWinsu2 小时前
对于Qt:QGripLayout常见属性、方法以及QSizePolicy的解析SimonKing2 小时前
给Deepseek Harness装上插件市场,从此插件无忧艾莉丝努力练剑2 小时前
【AI大模型接入SDK】C++的SessionManager 线程安全aiotto资源分享3 小时前
Python 写 JSON 文件,程序中途退出会留下什么?利威尔xu3 小时前
Go Mutex 饥饿模式:正常模式等待者饿死,1ms 阈值自动切换崩溃RuoyiOffice4 小时前
SpringBoot3+Vue3 采购到付款:ERP、WMS与财务如何按同一来源协同文丑颜不良啊4 小时前
LeetCode3261~3280题目解析代码不停4 小时前
Spring 原理