zoukankan      html  css  js  c++  java
  • 毕向东udp学习笔记3多线程聊天

    项目功能:

    实现了多线程下的发送接收,比较好

    希望可以加入GUI,类似聊天软件一样,有一个消息输入框,捕获输入消息,作为发送线程

                      有一个显示消息框,接收消息并显示,作为接收线程

    不知道的是,当在线程中使用UI的gettext(),settext()时,是否子线程和UI线程冲突,赶紧学习下。

    代码:

     1 import java.net.*;
     2 import java.io.*;
     3 /*
     4 编写一个聊天程序。
     5 有收数据的部分,和发数据的部分。
     6 这两部分需要同时执行。
     7 那就需要用到多线程技术。
     8 一个线程控制收,一个线程控制发。
     9 
    10 因为收和发动作是不一致的,所以要定义两个run方法。
    11 而且这两个方法要封装到不同的类中。
    12 
    13 */
    14  class Send implements Runnable
    15 {
    16     private DatagramSocket ds;
    17     //构造方法
    18     public Send(DatagramSocket ds){
    19         this.ds=ds;
    20     }
    21     
    22     public void run() {
    23 
    24         try{
    25             //使用控制台输入
    26             BufferedReader bufr = new  BufferedReader(new InputStreamReader(System.in));
    27             String line =null;
    28 
    29             while((line=bufr.readLine())!=null){
    30 
    31                 byte[] buf =line.getBytes();
    32                 DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.103"),10005);
    33                 
    34                 ds.send(dp);
    35                 //如果输入内容是"886",跳出循环
    36                 if("886".equals(line))
    37                     break;
    38             }
    39 
    40             //4.关闭连接
    41             ds.close();
    42 
    43         }catch(Exception e){
    44             throw new RuntimeException("发送端失败");
    45         }
    46     }
    47 }
    48 
    49  class Recv implements Runnable
    50 {
    51     private DatagramSocket ds;
    52     //构造方法
    53     public Recv(DatagramSocket ds){
    54         this.ds=ds;
    55     }
    56     
    57     public void run() {
    58         try{
    59             while(true){
    60                 //2,创建数据包
    61                 byte[] buf =new byte[1024];
    62                 DatagramPacket dp =new DatagramPacket(buf,buf.length);
    63 
    64                 //3,使用接收的方法将数据包存储到数据包中
    65                 ds.receive(dp);//阻塞式
    66 
    67                 //4.通过数据包对象的方法,解析其中的数据,比如端口,地址,内容等
    68                 String ip = dp.getAddress().getHostAddress();
    69                 int port  = dp.getPort();
    70                 String content = new String(dp.getData(),0,dp.getLength());
    71                 System.out.println(ip+"::" +port+":"+content);
    72                 if(content.equals("886")){
    73                     System.out.println(ip+"有人退出..." );
    74                     break;
    75                 }
    76                 
    77             }
    78             //满足886跳出循环后,退出
    79             ds.close();
    80         }catch(Exception e){
    81             throw new RuntimeException("接收端失败");
    82         }
    83     }
    84 }
    85 
    86 public class ChatDemo
    87 {
    88     public static void main(String[] args) throws IOException{
    89         DatagramSocket send =new DatagramSocket();
    90 
    91         DatagramSocket recv =new DatagramSocket(10005);
    92 
    93         new Thread(new Send(send)).start();
    94         new Thread(new Recv(recv)).start();
    95     }
    96 }

    注意:Send、Recv类前面是不能加修饰符public的,否则会报错

  • 相关阅读:
    Unique Binary Search Trees——LeetCode
    Binary Tree Inorder Traversal ——LeetCode
    Maximum Product Subarray——LeetCode
    Remove Linked List Elements——LeetCode
    Maximum Subarray——LeetCode
    Validate Binary Search Tree——LeetCode
    Swap Nodes in Pairs——LeetCode
    Find Minimum in Rotated Sorted Array——LeetCode
    Linked List Cycle——LeetCode
    VR AR MR
  • 原文地址:https://www.cnblogs.com/shuqingstudy/p/4928282.html
Copyright © 2011-2022 走看看