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
相关推荐
湫ccc5 分钟前
Python简介以及解释器安装(保姆级教学)程序伍六七8 分钟前
day16wkj00113 分钟前
php操作redis武子康15 分钟前
大数据-230 离线数仓 - ODS层的构建 Hive处理 UDF 与 SerDe 处理 与 当前总结武子康17 分钟前
大数据-231 离线数仓 - DWS 层、ADS 层的创建 Hive 执行脚本极客代码18 分钟前
【Python TensorFlow】进阶指南(续篇三)苏-言24 分钟前
Spring IOC实战指南:从零到一的构建过程土豆湿24 分钟前
拥抱极简主义前端开发:NoCss.js 引领无 CSS 编程潮流界面开发小八哥31 分钟前
更高效的Java 23开发,IntelliJ IDEA助力全面升级草莓base44 分钟前
【手写一个spring】spring源码的简单实现--容器启动