一、TCP实现小型聊天室

实现方法:服务器端接受所有的消息,然后把接收到的每一条消息发送给所有已连接的设备上去。

服务器端代码:

 1 package com.TCP;
 2 
 3 import java.util.*;
 4 
 5 
 6 import java.net.*;
 7 import java.io.*;
 8 public class TCPServer_2 {
 9     
10     private int no = 0; //为连接设备编号
11 
12     // 保存当前连接的客户端Socket
13     private List<Socket> sockList = Collections.synchronizedList(new ArrayList<Socket>());
14 
15     class ServerThread implements Runnable {
16         int no;
17         Socket s;
18         BufferedReader br;
19 
20         public ServerThread(Socket s, int no) {
21             this.s = s;
22             this.no = no;
23             try {
24                 br = new BufferedReader(new InputStreamReader(s.getInputStream()));
25             } catch (IOException e) {
26                 e.printStackTrace();
27             }
28         }
29 
30         private String getContentByLine() {
31             try {
32                 return br.readLine();
33             } catch (IOException e) { // 该请求可能已被客户端关闭
34                 e.printStackTrace();
35                 sockList.remove(s); // 因此需要从列表中移除
36             }
37             return null;
38         }
39 
40         @Override
41         public void run() {
42             String content = "";
43             while ((content = getContentByLine()) != null) {
44             // 只要客户端不关闭Socket,那么输入输出流就是一直开着的
45             // 服务器端会一直等待输入,永远也到不了EOF使getContentByLine返回null
46                 for (Socket s: sockList) { // 对每一个连接的客户端都发送聊天信息
47                     PrintStream ps;
48                     try {
49 //                        ps = new PrintStream(s.getOutputStream());
50 //                        ps.println("#" + no + " says: " + content);
51 //                        ps.println(x);
52                         DataOutputStream dataOut = new DataOutputStream(s.getOutputStream());
53                         if("CI".equals(content.substring(0, 2))){
54                             dataOut.writeBytes("SCXX" + '\n');
55                         }
56                         else if("CS".equals(content.substring(0, 2))){
57                             dataOut.writeBytes("SLXX" + '\n');
58                         }else{
59                             dataOut.writeBytes("#" + no + " says: " + content);
60                         }
61                     } catch (IOException e) {
62                         // TODO Auto-generated catch block
63                         e.printStackTrace();
64                     }
65                 }
66             }
67         }
68 
69     }
70     
71     public void init() throws IOException {
72 //        ServerSocket ss = new ServerSocket(8000, 10, InetAddress.getByName("192.168.96.1")); // 使用默认IP,端口3000
73         ServerSocket ss = new ServerSocket(8000);
74         while (true) {
75             Socket s = ss.accept();
76             no += 1;
77             System.out.println("#" + no + " connected!");
78             sockList.add(s);
79             new Thread(new ServerThread(s, no)).start();
80         }
81     }
82 
83     public static void main(String[] args) throws IOException {
84         new TCPServer_2().init();
85     }
86 
87 }
View Code

相关文章:

  • 2021-09-20
  • 2021-06-12
猜你喜欢
  • 2021-11-23
  • 2018-10-10
  • 2022-12-23
  • 2021-07-12
  • 2021-05-16
  • 2021-09-20
  • 2021-12-29
相关资源
相似解决方案