首页 > 其他 > 详细

网络编程-UDP

时间:2020-11-24 15:02:10      阅读:20      评论:0      收藏:0      [点我收藏+]

UDP发送和接收消息

  • 发送端

    package com.phil.socket;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    
    public class TestUDP1 {
        public static void main(String[] args) throws Exception {
            DatagramSocket socket = new DatagramSocket();
    
    //        String dataString = "你好  我是UDP1";
    
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            while (true) {
    
                String string = reader.readLine();
    
                DatagramPacket sendData = new DatagramPacket(string.getBytes(), 0, string.getBytes().length, InetAddress.getByName("localhost"), 8888);
    
                socket.send(sendData);
    
                if (string.equals("88"))
                    break;
            }
            socket.close();
        }
    }
    
  • 接收端

    package com.phil.socket;
    
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    
    public class TestUDP2 {
        public static void main(String[] args) throws Exception {
            DatagramSocket socket = new DatagramSocket(8888);
    
            byte[] buffer = new byte[1024];
    
            DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
    
    
            while (true) {
                socket.receive(packet);
    
                String s = new String(packet.getData());
                System.out.println(s);
    
                if (s.equals("88"))
                    break;
            }
    
            socket.close();
        }
    }
    

UDP实现多线程聊天

  • 发送工具类

    package com.phil.socket;
    
    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.InetSocketAddress;
    
    public class SendTool implements Runnable {
    
        private DatagramSocket socket = null;
        private BufferedReader reader = null;
    
        private int fromPort;
        private String toIP;
        private int toPort;
    
        public SendTool(int fromPort, String toIP, int toPort) {
            this.fromPort = fromPort;
            this.toIP = toIP;
            this.toPort = toPort;
    
            try {
                this.socket = new DatagramSocket(this.fromPort);
            }catch (Exception e) {
                e.printStackTrace();
            }
            this.reader = new BufferedReader(new InputStreamReader(System.in));
        }
    
        @Override
        public void run() {
            while (true) {
    
                String dataString = null;
                try {
                    dataString = this.reader.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                byte[] bytes = dataString.getBytes();
                DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, new InetSocketAddress(this.toIP, this.toPort));
    
                try {
                    this.socket.send(packet);
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                if (dataString.equals("88"))
                    break;
            }
    
            this.socket.close();
        }
    }
    
  • 接收工具类

    package com.phil.socket;
    
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    
    public class RecieveTool implements Runnable {
    
        private DatagramSocket socket = null;
        private int port;
        private String name;
    
        public RecieveTool(int port, String name) {
            this.port = port;
            this.name = name;
    
            try {
                this.socket = new DatagramSocket(port);
            }catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        @Override
        public void run() {
            while (true) {
                try {
                    byte[] buffer = new byte[1024];
                    DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
                    if (this.socket==null) {
                        System.out.println("傻逼哦");
                        break;
                    }
                    this.socket.receive(packet);
                    String dataString = new String(packet.getData());
                    System.out.println(this.name + ": " + dataString);
                    if (dataString.equals("88"))
                        break;
    
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
            this.socket.close();
        }
    }
    
  • 角色1(儿子)

    package com.phil.socket;
    
    public class Son {
        public static void main(String[] args) {
            new Thread(new SendTool(6666, "127.0.0.1", 7788)).start();
            new Thread(new RecieveTool(6677, "老子")).start();
        }
    }
    
  • 角色2(老子)

    package com.phil.socket;
    
    public class Father {
        public static void main(String[] args) {
            new Thread(new SendTool(7777,"localhost",6677)).start();
            new Thread(new RecieveTool(7788,"儿子")).start();
        }
    }
    

网络编程-UDP

原文:https://www.cnblogs.com/philx/p/14029889.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!