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
相关推荐
blammmp12 分钟前
Java:数据结构-枚举何曾参静谧24 分钟前
「C/C++」C/C++ 指针篇 之 指针运算暗黑起源喵30 分钟前
设计模式-工厂设计模式WaaTong35 分钟前
Java反射Troc_wangpeng36 分钟前
R language 关于二维平面直角坐标系的制作努力的家伙是不讨厌的38 分钟前
解析json导出csv或者直接入库Envyᥫᩣ1 小时前
C#语言:从入门到精通九圣残炎1 小时前
【从零开始的LeetCode-算法】1456. 定长子串中元音的最大数目wclass-zhengge1 小时前
Netty篇(入门编程)童先生1 小时前
Go 项目中实现类似 Java Shiro 的权限控制中间件?