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
相关推荐
灵槐梦15 分钟前
【速成51单片机】2.点亮LED想睡觉 . 我也想睡觉 .16 分钟前
【C++算法】1.【模板】前缀和it噩梦20 分钟前
深度分析 es multi_match 中most_fields、best_fields、cross_fields区别好看资源平台21 分钟前
Java Web开发基础——Web应用的请求与响应机制刘Java24 分钟前
Dubbo 3.x源码(28)—Dubbo服务发布导出源码(7)应用级服务接口元数据发布---wzy---24 分钟前
我的JAVA-Web基础(2)伟大无须多言35 分钟前
企业资源规划系统(ERP)服务器上线项目实施指南逊嘘37 分钟前
【Java数据结构】LinkedList周盛欢37 分钟前
云服务器yum无法解析mirrorlist.centos.org总是学不会.38 分钟前
第五篇:前后端如何“扯皮”——HTTP 在开发中的应用