Java Socket实现简易聊天室

时间:2022-09-23 08:51:22

Java-Socket编程实现简易聊天室(TCP),供大家参考,具体内容如下

实现一个服务器接收多个客户端

测试:

首先启动服务器,然后启动三个客户端,输入三个不同的用户名,分别在聊天室发消息
看其他客户端是否能接收到消息

效果如下图:

一号发消息

Java Socket实现简易聊天室

可以在二号和三号客户端接收到消息

Java Socket实现简易聊天室

Java Socket实现简易聊天室

服务器设计

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.concurrent.ExecutorService;
  10. import java.util.concurrent.Executors;
  11.  
  12. public class Server1 {
  13.  
  14. static public List<Client> list = new ArrayList<Client>();
  15.  
  16. static ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
  17.  
  18. public static void main(String[] args) {
  19. //创建一个ServerSocket监听端口
  20. try (ServerSocket server = new ServerSocket(5678)) {
  21.  
  22. while (true) { // 利用死循环不停的监听端口
  23.  
  24. Socket s = server.accept();// 利用Socket服务器的accept()方法获取客户端Socket对象。
  25. addClient(s);
  26.  
  27. }
  28.  
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33.  
  34. // 添加客户端
  35. private static void addClient(Socket s) {
  36. String name;
  37.  
  38. try {
  39. BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
  40. name = in.readLine();
  41. Client c = new Client(name, s);// 创建客户端处理线程对象
  42. System.out.println(name);
  43. list.add(c);
  44.  
  45. cachedThreadPool.execute(c);
  46.  
  47. } catch (IOException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51.  
  52. static class Client implements Runnable {
  53.  
  54. String name; // 客户端名字
  55. Socket s = null; // 保存客户端Socket对象
  56.  
  57. BufferedReader in;
  58. PrintWriter out;
  59.  
  60. Client(String name, Socket s) {
  61.  
  62. this.s = s;
  63. this.name = name;
  64.  
  65. try {
  66. in = new BufferedReader(new InputStreamReader(s.getInputStream()));
  67. out = new PrintWriter(s.getOutputStream());
  68. } catch (IOException e) {
  69. // TODO Auto-generated catch block
  70. e.printStackTrace();
  71. }
  72. }
  73.  
  74. @Override
  75. public void run() {
  76. try {
  77. while (true) {
  78. String str = in.readLine();
  79. for (int j = 0; j < list.size(); j++) {
  80. Server1.Client c = list.get(j);
  81. if (c != this) {
  82. System.out.println(str);
  83. c.send(str+"-|1|2|-"+name);
  84. }
  85. }
  86.  
  87. if (str.equals("end"))
  88. break;
  89. }
  90.  
  91. try {
  92. s.close();
  93. } catch (IOException e) {
  94. // TODO Auto-generated catch block
  95. e.printStackTrace();
  96. }
  97. } catch (IOException e1) {
  98. // TODO Auto-generated catch block
  99. e1.printStackTrace();
  100. }
  101. }
  102.  
  103. public void send(String str) throws IOException {
  104. out.println(str);
  105. out.flush();
  106. }
  107. }
  108. }

客户端设计

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.net.Socket;
  6. import java.util.Scanner;
  7.  
  8. public class Client1 {
  9. public static String name = null;
  10. static Socket socket = null;
  11.  
  12. static BufferedReader in;
  13. static PrintWriter out;
  14.  
  15. public static void main(String[] args) {
  16. Scanner sc = new Scanner(System.in);
  17.  
  18. System.out.print("输入用户名:>");
  19. name = sc.nextLine();
  20.  
  21. try {
  22. // socket = new Socket("116.62.173.212", 5678);
  23. socket = new Socket("127.0.0.1", 5678);
  24.  
  25. System.out.println(socket);
  26.  
  27. System.out.println("已连接服务器>");
  28.  
  29. out = new PrintWriter(socket.getOutputStream());
  30. in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  31.  
  32. Thread receive = new Thread(new receiveThread());
  33. receive.start();
  34.  
  35. out.println(name);
  36. out.flush();
  37.  
  38. String msg;
  39.  
  40. while (true) {
  41. // System.out.print("你说:>");
  42. msg = sc.nextLine();
  43.  
  44. out.println(msg);
  45. out.flush();
  46.  
  47. if (msg.equalsIgnoreCase("end"))
  48. break;
  49. }
  50.  
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55.  
  56. static class receiveThread implements Runnable{
  57.  
  58. @Override
  59. public void run() {
  60. try {
  61. while (socket!=null) {
  62.  
  63. String str = in.readLine();
  64. String message = str.split("-\\|1\\|2\\|-")[0];
  65. String name = str.split("-\\|1\\|2\\|-")[1];
  66. System.out.println(name + ":>" + message);
  67.  
  68. }
  69. }catch(Exception e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. }
  74. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/qq_42843894/article/details/114793283