• Java基础知识强化之网络编程笔记05:UDP之多线程实现聊天室案例


    1. 通过多线程改进刚才的聊天程序,这样我就可以实现在一个窗口发送和接收数据了

    2.  代码示例:

    (1)SendThread.java,如下:

     1 package com.himi.udpDemo2;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.IOException;
     5 import java.io.InputStreamReader;
     6 import java.net.DatagramPacket;
     7 import java.net.DatagramSocket;
     8 import java.net.InetAddress;
     9 
    10 public class SendThread implements Runnable {
    11 
    12     private DatagramSocket ds;
    13 
    14     public SendThread(DatagramSocket ds) {
    15         this.ds = ds;
    16     }
    17 
    18     public void run() {
    19         try {
    20             // 封装键盘录入数据
    21             BufferedReader br = new BufferedReader(new InputStreamReader(
    22                     System.in));
    23             String line = null;
    24             while ((line = br.readLine()) != null) {
    25                 if ("886".equals(line)) {
    26                     break;
    27                 }
    28 
    29                 // 创建数据并打包
    30                 byte[] bys = line.getBytes();
    31                 // DatagramPacket dp = new DatagramPacket(bys, bys.length,
    32                 // InetAddress.getByName("192.168.12.92"), 12345);
    33                 DatagramPacket dp = new DatagramPacket(bys, bys.length,
    34                         InetAddress.getByName("49.123.72.145"), 12306);
    35 
    36                 // 发送数据
    37                 ds.send(dp);
    38             }
    39 
    40             // 释放资源
    41             ds.close();
    42         } catch (IOException e) {
    43             e.printStackTrace();
    44         }
    45     }
    46 
    47 }

    (2)ReceiveThread.java,如下:

     1 package com.himi.udpDemo2;
     2 
     3 import java.io.IOException;
     4 import java.net.DatagramPacket;
     5 import java.net.DatagramSocket;
     6 
     7 public class ReceiveThread implements Runnable {
     8     private DatagramSocket ds;
     9 
    10     public ReceiveThread(DatagramSocket ds) {
    11         this.ds = ds;
    12     }
    13 
    14     public void run() {
    15         try {
    16             while (true) {
    17                 // 创建一个包裹
    18                 byte[] bys = new byte[1024];
    19                 DatagramPacket dp = new DatagramPacket(bys, bys.length);
    20 
    21                 // 接收数据
    22                 ds.receive(dp);
    23 
    24                 // 解析数据
    25                 String ip = dp.getAddress().getHostAddress();
    26                 String s = new String(dp.getData(), 0, dp.getLength());
    27                 System.out.println("from " + ip + " data is : " + s);
    28             }
    29         } catch (IOException e) {
    30             e.printStackTrace();
    31         }
    32     }
    33 
    34 }

    (3)聊天室程序CharRoom.java,调用上面两个线程工具类,代码如下:

     1 package com.himi.udpDemo2;
     2 
     3 import java.io.IOException;
     4 import java.net.DatagramSocket;
     5 
     6 /*
     7  * 通过多线程改进刚才的聊天程序,这样我就可以实现在一个窗口发送和接收数据了
     8  */
     9 public class ChatRoom {
    10     public static void main(String[] args) throws IOException {
    11         DatagramSocket dsSend = new DatagramSocket();
    12         DatagramSocket dsReceive = new DatagramSocket(12306);
    13 
    14         SendThread st = new SendThread(dsSend);
    15         ReceiveThread rt = new ReceiveThread(dsReceive);
    16 
    17         Thread t1 = new Thread(st);
    18         Thread t2 = new Thread(rt);
    19 
    20         t1.start();
    21         t2.start();
    22     }
    23 }

    运行效果如下:

  • 相关阅读:
    STM32 USART整理说明(转)
    C++ 如何初始化静态类成员
    scp、sftp和ftps
    PostGIS介绍
    string.h和strings.h的区别
    linux编程中的段错误
    Linux中的man命令
    undefinded reference to 'pthread_create'问题
    多核编程框架
    与ComboBox有相似行为的下拉控件的实现
  • 原文地址:https://www.cnblogs.com/hebao0514/p/4859593.html
Copyright © 2020-2023  润新知