• UDP聊天


    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    
    public class ChatDemo {
    
        public static void main(String[] args) throws SocketException {
    
            /*
             * 通过UDP协议 完成一个聊天程序。 一个负责发送数据的任务,一个负责接收数据的任务 两个任务需要同时执行,可以使用多线程技术
             */
            
            //创建socket服务
            //发送端
            DatagramSocket send = new DatagramSocket(8888);
            DatagramSocket rece = new DatagramSocket(10001);
            
            new Thread(new Send(send)).start();
            new Thread(new Rece(rece)).start();
            
    
        }
    
    }
    
    // 负责发送的任务,通过UDP的socket发送
    class Send implements Runnable {
        // 任务对象一建立,需要socket对象
        private DatagramSocket ds;
    
        public Send(DatagramSocket ds) {
            super();
            this.ds = ds;
        }
    
        @Override
        public void run() {
            // 具体的发送数据的任务内容
            // 1.要发送的数据来自哪里?键盘录入
            BufferedReader bufr = new BufferedReader(new InputStreamReader(
                    System.in));
    
            // 1.1读取数据
            String line = null;
            try {
                while ((line = bufr.readLine()) != null) {
    
                    // 1.2将数据封装成字节数组
                    // 2.将数据封装到数据包中。
                    byte[] buf = line.getBytes();
                    DatagramPacket dp = new DatagramPacket(buf, buf.length,
                            InetAddress.getByName("192.168.17.255"), 10001);//255是广播,在该网路段的所有主机都能收到
    
                    // 3.将数据包发送出去。
                    ds.send(dp);
    
                    if ("over".equals(line)) {
                        break;
                    }
                }
                ds.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
    }
    
    // 负责接收的任务
    class Rece implements Runnable {
    
        private DatagramSocket ds;
    
        public Rece(DatagramSocket ds) {
            super();
            this.ds = ds;
        }
    
        @Override
        public void run() {
            while (true) {
                // 接收的具体的任务内容
                // 1.因为接收的数据最终都会存储到数据包中,而数据包中必须有字节数组。
                byte[] buf = new byte[1024];
    
                // 2.创建数据包对象
                DatagramPacket dp = new DatagramPacket(buf, buf.length);
    
                // 3.将数据存储到数据包中
                try {
                    ds.receive(dp);
                    // 4.获取数据
                    String ip = dp.getAddress().getHostAddress();
    
                    String data = new String(dp.getData(), 0, dp.getLength());
                    
                    System.out.println(ip + ":" + data);
                    
                    if("over".equals(data)){
                        System.out.println(ip+"......离开聊天室");
                    }
    
    
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
        }
    
    }
  • 相关阅读:
    ValidationUtils 验证工具
    android获取textview的行数
    自动打开Accesibility Service 可以自动安装APP
    限制EditText最多输入多少汉字
    Android Secret Code
    恢复 混淆后的 stacktrace 文件
    车载系统 降噪处理
    Java常用类库Integer,Character,toCharArray
    Java接口以及匿名内部类,静态代码块
    Java抽象类、继承及多态和适配器的实现
  • 原文地址:https://www.cnblogs.com/qjlbky/p/5931223.html
Copyright © 2020-2023  润新知