千家信息网

java Socket多线程服务器源代码是怎样的

发表于:2024-11-23 作者:千家信息网编辑
千家信息网最后更新 2024年11月23日,今天就跟大家聊聊有关java Socket多线程服务器源代码是怎样的,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。java Socket多线程
千家信息网最后更新 2024年11月23日java Socket多线程服务器源代码是怎样的

今天就跟大家聊聊有关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. public class ChatServer {

  12. private static final int PORT = 1234; // 端口号

  13. private static List list = new ArrayList();
    // 保存连接对象

  14. private ExecutorService exec;

  15. private ServerSocket server;

  16. public static void main(String[] args) {

  17. new ChatServer();

  18. }

  19. public ChatServer() {

  20. try {

  21. server = new ServerSocket(PORT);

  22. exec = Executors.newCachedThreadPool();

  23. System.out.println("服务器已启动!");

  24. Socket client = null;

  25. while (true) {

  26. client = server.accept(); // 接收客户连接

  27. list.add(client);

  28. exec.execute(new ChatTask(client));

  29. }

  30. } catch (IOException e) {

  31. e.printStackTrace();

  32. }

  33. }

  34. static class ChatTask implements Runnable {

  35. private Socket socket;

  36. private BufferedReader br;

  37. private PrintWriter pw;

  38. private String msg;

  39. public ChatTask(Socket socket) throws IOException {

  40. this.socket = socket;

  41. br = new BufferedReader(new InputStreamReader(socket

  42. .getInputStream()));

  43. msg = "【" + this.socket.getInetAddress() + "】
    进入聊天室!当前聊天室有【"

  44. + list.size() + "】人";

  45. sendMessage();

  46. }

  47. public void run() {

  48. try {

  49. while ((msg = br.readLine()) != null) {

  50. if (msg.trim().equals("bye")) {

  51. list.remove(socket);

  52. br.close();

  53. pw.close();

  54. msg = "【" + socket.getInetAddress() + "】
    离开聊天室!当前聊天室有【"

  55. + list.size() + "】人";

  56. socket.close();

  57. sendMessage();

  58. break;

  59. } else {

  60. msg = "【" + socket.getInetAddress() + "】说:" + msg;

  61. sendMessage();

  62. }

  63. }

  64. } catch (IOException e) {

  65. e.printStackTrace();

  66. }

  67. }

  68. /**

  69. * 群发消息给聊天室的所有人

  70. */

  71. private void sendMessage() throws IOException {

  72. System.out.println(msg);

  73. for (Socket client : list) {

  74. pw = new PrintWriter(client.getOutputStream(), true);

  75. pw.println(msg);

  76. }

  77. }

  78. }

  79. }

  80. import java.io.BufferedReader;

  81. import java.io.InputStreamReader;

  82. import java.io.PrintWriter;

  83. import java.net.Socket;

  84. import java.util.concurrent.ExecutorService;

  85. import java.util.concurrent.Executors;

  86. public class ChatClient {

  87. private static final int PORT = 7891;

  88. private static ExecutorService exec =
    Executors.newCachedThreadPool();

  89. public static void main(String[] args) throws Exception {

  90. new ChatClient();

  91. }

  92. public ChatClient() {

  93. try {

  94. Socket socket = new Socket("221.130.165.41″, PORT);

  95. exec.execute(new Sender(socket));

  96. System.out.println("【" + socket.getInetAddress() + "】
    您好,欢迎来到简易聊天室!");

  97. BufferedReader br = new BufferedReader(new
    InputStreamReader(socket.getInputStream()));

  98. String msg;

  99. while ((msg = br.readLine()) != null) {

  100. System.out.println("收到—->"+msg);

  101. }

  102. } catch (Exception e) {

  103. }

  104. }

看完上述内容,你们对java Socket多线程服务器源代码是怎样的有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

0