首页 > 代码库 > UDP传输

UDP传输

  • 1.发送Send
    • 创建DatagramSocket, 随机端口号
    • 创建DatagramPacket, 指定数据, 长度, 地址, 端口
    • 使用DatagramSocket发送DatagramPacket
    • 关闭DatagramSocket
  • 2.接收Receive
    • 创建DatagramSocket, 指定端口号
    • 创建DatagramPacket, 指定数组, 长度
    • 使用DatagramSocket接收DatagramPacket
    • 关闭DatagramSocket
    • 从DatagramPacket中获取数据
  • 3.接收方获取ip和端口号
    • String ip = packet.getAddress().getHostAddress();
    • int port = packet.getPort();

 


UDP传输优化

  • 接收端Receive
  • DatagramSocket socket = new DatagramSocket(6666);                       //创建socket相当于创建码头
    DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);       //创建packet相当于创建集装箱
    
    while(true) {
        socket.receive(packet);                                             //接收货物
         byte[] arr = packet.getData();
        int len = packet.getLength();
        String ip = packet.getAddress().getHostAddress();
        System.out.println(ip + ":" + new String(arr,0,len));
    }
    

     

     
  • 发送端Send

    DatagramSocket socket = new DatagramSocket();       //创建socket相当于创建码头
    Scanner sc = new Scanner(System.in);
    
    while(true) {
        String str = sc.nextLine();
        if("quit".equals(str))
            break;
        DatagramPacket packet =                             //创建packet相当于创建集装箱
                new DatagramPacket(str.getBytes(), str.getBytes().length, InetAddress.getByName("127.0.0.1"), 6666);
        socket.send(packet);            //发货
    }
    socket.close();
    

     

     

 


 

UDP传输多线程

发送和接收在一个窗口完成

public class Demo_MoreThread {

    /**
     * @param args
     */
    public static void main(String[] args) {
        new Receive().start();

        new Send().start();
    }

}

class Receive extends Thread {
    public void run() {
        try {
            DatagramSocket socket = new DatagramSocket(6666);                   //创建socket相当于创建码头
            DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);   //创建packet相当于创建集装箱

            while(true) {
                socket.receive(packet);                                             //接收货物
                byte[] arr = packet.getData();
                int len = packet.getLength();
                String ip = packet.getAddress().getHostAddress();
                System.out.println(ip + ":" + new String(arr,0,len));
            }
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}

class Send extends Thread {
    public void run() {
        try {
            DatagramSocket socket = new DatagramSocket();       //创建socket相当于创建码头
            Scanner sc = new Scanner(System.in);

            while(true) {
                String str = sc.nextLine();
                if("quit".equals(str))
                    break;
                DatagramPacket packet =                             //创建packet相当于创建集装箱
                        new DatagramPacket(str.getBytes(), str.getBytes().length, InetAddress.getByName("127.0.0.1"), 6666);
                socket.send(packet);            //发货
            }
            socket.close();
        }  catch (IOException e) {

            e.printStackTrace();
        }
    }
}

 

 

UDP传输