JAVA基础(UDP传输)

1,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();

接收:

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.SocketException;





public class Demo1_Receive {





    /**

     ** 2.接收Receive

     * 创建DatagramSocket, 指定端口号

     * 创建DatagramPacket, 指定数组, 长度

     * 使用DatagramSocket接收DatagramPacket

     * 关闭DatagramSocket

     * 从DatagramPacket中获取数据

     * @throws Exception

     */

    public static void main(String[] args) throws Exception {

        DatagramSocket socket = new DatagramSocket(6666);        //创建Socket相当于创建码头

        DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);//创建Packet相当于创建集装箱

        socket.receive(packet);                                    //接货,接收数据

        

        byte[] arr = packet.getData();                            //获取数据

        int len = packet.getLength();                            //获取有效的字节个数

        System.out.println(new String(arr,0,len));

        socket.close();

    }

}

 

发送:

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

import java.net.SocketException;

import java.net.UnknownHostException;





public class Demo1_Send {





    /**

     * * 1.发送Send

     * 创建DatagramSocket, 随机端口号

     * 创建DatagramPacket, 指定数据, 长度, 地址, 端口

     * 使用DatagramSocket发送DatagramPacket

     * 关闭DatagramSocket

     * @throws SocketException

     * @throws UnknownHostException

     */

    public static void main(String[] args) throws Exception {

        String str = "what are you 弄啥呢?";

        DatagramSocket socket = new DatagramSocket();                //创建Socket相当于创建码头

        DatagramPacket packet =                                     //创建Packet相当于集装箱

                new DatagramPacket(str.getBytes(), str.getBytes().length, InetAddress.getByName("127.0.0.1"), 6666);

        socket.send(packet);                                        //发货,将数据发出去

        socket.close();                                                //关闭码头

    }





}



 

 

JAVA基础(UDP传输)_第1张图片

 

 

 

2,UDP传输优化

  • 接收端Receive

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.SocketException;

public class Demo2_Receive {

    /**

     ** 2.接收Receive

     * 创建DatagramSocket, 指定端口号

     * 创建DatagramPacket, 指定数组, 长度

     * 使用DatagramSocket接收DatagramPacket

     * 关闭DatagramSocket

     * 从DatagramPacket中获取数据

     * @throws Exception

     */

    public static void main(String[] args) throws Exception {

        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();        //获取ip地址

            int port = packet.getPort();                            //获取端口号

            System.out.println(ip + ":" + port + ":" + new String(arr,0,len));

        }

    }

}

发送端Send





import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

import java.net.SocketException;

import java.net.UnknownHostException;

import java.util.Scanner;





public class Demo2_Send {





    /**

     * * 1.发送Send

     * 创建DatagramSocket, 随机端口号

     * 创建DatagramPacket, 指定数据, 长度, 地址, 端口

     * 使用DatagramSocket发送DatagramPacket

     * 关闭DatagramSocket

     * @throws SocketException

     * @throws UnknownHostException

     */

    public static void main(String[] args) throws Exception {

        Scanner sc = new Scanner(System.in);                        //创建键盘录入对象

        DatagramSocket socket = new DatagramSocket();                //创建Socket相当于创建码头

        

        while(true) {

            String line = sc.nextLine();                            //获取键盘录入的字符串

            if("quit".equals(line)) {

                break;

            }

            DatagramPacket packet =                                 //创建Packet相当于集装箱

                    new DatagramPacket(line.getBytes(), line.getBytes().length, InetAddress.getByName("127.0.0.1"), 6666);

            socket.send(packet);                                    //发货,将数据发出去

        }

        socket.close();                                                //关闭码头

    }





}

 

 

 2,UDP传输多线程)

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

import java.io.IOException;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

import java.net.SocketException;

import java.net.UnknownHostException;

import java.util.Scanner;





public class Demo3_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();        //获取ip地址

                int port = packet.getPort();                            //获取端口号

                System.out.println(ip + ":" + port + ":" + new String(arr,0,len));

            }

        } catch (IOException e) {

            

            e.printStackTrace();

        }

    }

}





class Send extends Thread {

    public void run() {

        try {

            Scanner sc = new Scanner(System.in);                        //创建键盘录入对象

            DatagramSocket socket = new DatagramSocket();                //创建Socket相当于创建码头

            

            while(true) {

                String line = sc.nextLine();                            //获取键盘录入的字符串

                if("quit".equals(line)) {

                    break;

                }

                DatagramPacket packet =                                 //创建Packet相当于集装箱

                        new DatagramPacket(line.getBytes(), line.getBytes().length, InetAddress.getByName("127.0.0.1"), 6666);

                socket.send(packet);                                    //发货,将数据发出去

            }

            socket.close();

        }  catch (IOException e) {

            

            e.printStackTrace();

        }        

    }

}

 

 

你可能感兴趣的:(JAVA基础(UDP传输))