TCP实现通信,单发、多发、多客户端发送、线程池、群发转发

 1 public static void main(String[] args) {
 2         //  单个客户端单条发送
 3         System.out.println("=============客户端=============");
 4         try {
 5             //  创建管道连接请求服务器连接
 6             // Socket(String host, int port)
 7             Socket socket = new Socket("127.0.0.1",6666);
 8             //  从Socket管道中得到一个字节输出流向服务器端发送消息
 9             OutputStream os = socket.getOutputStream();
10             //  将低级流包装成打印流
11             PrintStream ps = new PrintStream(os);
12             //  发送消息
13             ps.println("天王盖地虎~");
14             //  刷新
15             ps.flush();
16         } catch (Exception e) {
17             e.printStackTrace();
18         }
19     }

 1 public static void main(String[] args) {
 2         try {
 3             System.out.println("=============服务器端=============");
 4             //  注册端口
 5             ServerSocket serverSocket = new ServerSocket(6666);
 6             //  调用accept方法,等待接收客户端消息,简历Socket管道
 7             Socket accept = serverSocket.accept();
 8             //  从Socket管道得到一个字节流消息
 9             InputStream is = accept.getInputStream();
10             //  把字符输入流包装成缓冲流
11             BufferedReader br = new BufferedReader(new InputStreamReader(is));
12             //  按照行读取消息
13             String msg;
14             if ((msg = br.readLine()) != null){
15                 System.out.println(accept.getRemoteSocketAddress() + "说:" + msg);
16             }
17         } catch (Exception e) {
18             e.printStackTrace();
19         }
20     }
 1  public static void main(String[] args) {
 2         System.out.println("=============客户端=============");
 3         //  单个客户端多次发送
 4         try {
 5             //  创建管道连接请求服务器连接
 6             // Socket(String host, int port)
 7             Socket socket = new Socket("127.0.0.1",6666);
 8             //  从Socket管道中得到一个字节输出流向服务器端发送消息
 9             OutputStream os = socket.getOutputStream();
10             //  将低级流包装成打印流
11             PrintStream ps = new PrintStream(os);
12 
13             Scanner sc = new Scanner(System.in);
14             while (true) {
15                 System.out.println("说:");
16                 String msg = sc.next();
17                 //  发送消息
18                 ps.println(msg);
19                 //  刷新
20                 ps.flush();
21             }
22         } catch (Exception e) {
23             e.printStackTrace();
24         }
25     }

 1 public static void main(String[] args) {
 2         try {
 3             System.out.println("=============服务器端=============");
 4             //  注册端口
 5             ServerSocket serverSocket = new ServerSocket(6666);
 6             //  调用accept方法,等待接收客户端消息,简历Socket管道
 7             while (true) {
 8                 Socket accept = serverSocket.accept();
 9                 //  从Socket管道得到一个字节流消息
10                 InputStream is = accept.getInputStream();
11                 //  把字符输入流包装成缓冲流
12                 BufferedReader br = new BufferedReader(new InputStreamReader(is));
13                 //  按照行读取消息
14                 String msg;
15                 while ((msg = br.readLine()) != null){
16                     System.out.println(accept.getRemoteSocketAddress() + "说:" + msg);
17                 }
18             }
19         } catch (Exception e) {
20             e.printStackTrace();
21         }
22     }

 1 public static void main(String[] args) {
 2         System.out.println("=============客户端=============");
 3         //  多个客户端多次发送
 4         try {
 5             //  创建管道连接请求服务器连接
 6             // Socket(String host, int port)
 7             Socket socket = new Socket("127.0.0.1",6666);
 8             //  从Socket管道中得到一个字节输出流向服务器端发送消息
 9             OutputStream os = socket.getOutputStream();
10             //  将低级流包装成打印流
11             PrintStream ps = new PrintStream(os);
12 
13             Scanner sc = new Scanner(System.in);
14             while (true) {
15                 System.out.println("请说:");
16                 String msg = sc.nextLine();
17                 //  发送消息
18                 ps.println(msg);
19                 //  刷新
20                 ps.flush();
21             }
22         } catch (Exception e) {
23             e.printStackTrace();
24         }
25     }

 1 public static void main(String[] args) {
 2         
 3         try {
 4             System.out.println("=============服务器端=============");
 5             //  注册端口
 6             ServerSocket serverSocket = new ServerSocket(6666);
 7             while (true) {
 8                 //  没接收到一个消息就交给一个管道
 9                 Socket socket = serverSocket.accept();
10                 System.out.println(socket.getRemoteSocketAddress() + "上线了");
11                 //  创建独立线程处理 Socket 消息
12                 new ServerReaderThread(socket).start();
13             }
14         } catch (Exception e) {
15             e.printStackTrace();
16         }
17     }
 1 //  线程
 2 public class ServerReaderThread  extends Thread{
 3     private Socket socket;
 4     public ServerReaderThread(Socket socket){
 5         this.socket = socket;
 6     }
 7     @Override
 8     public void run() {
 9         try {
10             //  从Socket管道得到一个字节流消息
11             InputStream is = socket.getInputStream();
12             //  把字符输入流包装成缓冲流
13             BufferedReader br = new BufferedReader(new InputStreamReader(is));
14             //  按照行读取消息
15             String msg;
16             while ((msg = br.readLine()) != null){
17                 System.out.println(socket.getRemoteSocketAddress() + "说:" + msg);
18             }
19         } catch (Exception e) {
20             System.out.println(socket.getRemoteSocketAddress() + "下线了~~");
21         }
22     }
23 }

 

 1 public static void main(String[] args) {
 2         System.out.println("=============客户端=============");
 3         //  多个客户端多次发送
 4         try {
 5             //  创建管道连接请求服务器连接
 6             // Socket(String host, int port)
 7             Socket socket = new Socket("127.0.0.1",6666);
 8             //  从Socket管道中得到一个字节输出流向服务器端发送消息
 9             OutputStream os = socket.getOutputStream();
10             //  将低级流包装成打印流
11             PrintStream ps = new PrintStream(os);
12 
13             Scanner sc = new Scanner(System.in);
14             while (true) {
15                 System.out.println("请说:");
16                 String msg = sc.nextLine();
17                 //  发送消息
18                 ps.println(msg);
19                 //  刷新
20                 ps.flush();
21             }
22         } catch (Exception e) {
23             e.printStackTrace();
24         }
25     }

 1 /*
 2         线程池的七大参数
 3         1.corePoolSize 线程池核心线程大小
 4         2.maximumPoolSize 线程池最大线程数量
 5         3.keepAliveTime 空闲线程存活时间
 6         4.TimeUnit:
 7             unit 空闲线程存活时间单位
 8             keepAliveTime的计量单位
 9         5.workQueue 工作队列
10         6.threadFactory 线程工厂
11         7.handler 拒绝策略
12      */
13     //  创建线程池对象
14     private  static ExecutorService pool = new ThreadPoolExecutor(3, 5, 6,
15             TimeUnit.SECONDS, new ArrayBlockingQueue<>(2), Executors.defaultThreadFactory(),
16             new ThreadPoolExecutor.AbortPolicy());
17 
18     public static void main(String[] args) {
19         try {
20             System.out.println("=============服务器端=============");
21             //  注册端口
22             ServerSocket serverSocket = new ServerSocket(6666);
23             while (true) {
24                 //  没接收到一个消息就交给一个管道
25                 Socket socket = serverSocket.accept();
26                 System.out.println(socket.getRemoteSocketAddress() + "上线了");
27                 //  负责读取消息
28                 pool.execute(new ServerSocketRunnable(socket));
29             }
30         } catch (Exception e) {
31             e.printStackTrace();
32         }
33     }
 1 public class ServerSocketRunnable implements Runnable {
 2     private Socket socket;
 3     public ServerSocketRunnable(Socket socket){
 4         this.socket = socket;
 5     }
 6     @Override
 7     public void run() {
 8         try {
 9             //  从Socket管道得到一个字节流消息
10             InputStream is = socket.getInputStream();
11             //  把字符输入流包装成缓冲流
12             BufferedReader br = new BufferedReader(new InputStreamReader(is));
13             //  按照行读取消息
14             String msg;
15             while ((msg = br.readLine()) != null){
16                 System.out.println(socket.getRemoteSocketAddress() + "说:" + msg);
17             }
18         } catch (Exception e) {
19             System.out.println(socket.getRemoteSocketAddress() + "下线了~~");
20         }
21     }
22 }

 

 1 public class ClientDemo {
 2     //  TCP通信群发转发
 3     public static void main(String[] args) throws Exception {
 4     System.out.println("=============客户端=============");
 5     //  创建管道连接请求服务器连接
 6     // Socket(String host, int port)
 7     Socket socket = new Socket("127.0.0.1",6666);
 8     //  创建独立线程专门负责客户端消息(服务器端可能随时发送消息过来
 9     new ClientReaderThread(socket).start();
10     //  从Socket管道中得到一个字节输出流向服务器端发送消息
11     OutputStream os = socket.getOutputStream();
12     //  将低级流包装成打印流
13     PrintStream ps = new PrintStream(os);
14 
15     Scanner sc = new Scanner(System.in);
16     while (true) {
17         String msg = sc.nextLine();
18         //  发送消息
19         ps.println(msg);
20         //  刷新
21         ps.flush();
22         }
23     }
24 }
25 
26 class ClientReaderThread extends Thread{
27     private Socket socket;
28     public ClientReaderThread(Socket socket){
29         this.socket = socket;
30     }
31     @Override
32     public void run() {
33         try {
34             //  从Socket管道得到一个字节流消息
35             InputStream is = socket.getInputStream();
36             //  把字符输入流包装成缓冲流
37             BufferedReader br = new BufferedReader(new InputStreamReader(is));
38             //  按照行读取消息
39             String msg;
40             while ((msg = br.readLine()) != null){
41                 System.out.println("收到消息:" + msg);
42             }
43         } catch (Exception e) {
44             System.out.println("下线了~~");
45         }
46     }
47 }

 1 public class ServerSocketDemo {
 2     //  定义一个集合存储全部在线的Socket
 3     public static List<Socket> allOnlineSockets = new ArrayList<>();
 4     public static void main(String[] args) throws Exception {
 5         System.out.println("=============服务器端=============");
 6         //  注册端口
 7         ServerSocket serverSocket = new ServerSocket(6666);
 8         while (true) {
 9             //  没接收到一个消息就交给一个管道
10             Socket socket = serverSocket.accept();
11             System.out.println(socket.getRemoteSocketAddress() + "上线了");
12             allOnlineSockets.add(socket);
13             //  创建独立线程处理 Socket 消息
14             new ServerReaderThread(socket).start();
15         }
16     }
17 }
18 
19 class ServerReaderThread extends Thread{
20     private Socket socket;
21     public ServerReaderThread(Socket socket){
22         this.socket = socket;
23     }
24     @Override
25     public void run() {
26         try {
27             //  从Socket管道得到一个字节流消息
28             InputStream is = socket.getInputStream();
29             //  把字符输入流包装成缓冲流
30             BufferedReader br = new BufferedReader(new InputStreamReader(is));
31             //  按照行读取消息
32             String msg;
33             while ((msg = br.readLine()) != null){
34                 System.out.println(socket.getRemoteSocketAddress() + "说了:" + msg);
35                 //  把这个消息发送给所有管道
36                 sendMsgToAll(msg);
37             }
38         } catch (Exception e) {
39             System.out.println(socket.getRemoteSocketAddress() + "下线了~~");
40             ServerSocketDemo.allOnlineSockets.remove(socket);
41         }
42     }
43 
44     private void sendMsgToAll(String msg) throws Exception {
45         for (Socket socket : ServerSocketDemo.allOnlineSockets) {
46             PrintStream ps = new PrintStream(socket.getOutputStream());
47             ps.println(msg);
48             ps.flush();
49         }
50     }
51 }

 

posted @ 2024-01-31 02:49  小※兽  阅读(153)  评论(0编辑  收藏  举报