UDP protocol

udp:

UDP is the abbreviation of User Datagram Protocol, and the Chinese name is User Datagram Protocol. It is a connectionless transport layer protocol in the OSI (Open System Interconnection) reference model, providing transaction-oriented simple and unreliable information transmission services. , IETF RFC 768 is the official specification for UDP. The protocol number of UDP in IP packets is 17. 
The full name of the UDP protocol is the User Datagram Protocol [1]. It is used to process data packets in the same network as the TCP protocol, and is a connectionless protocol. In the OSI model, the fourth layer, the transport layer, is the upper layer of the IP protocol. UDP has the disadvantage of not providing packet grouping, assembly and sorting of packets, that is to say, when a packet is sent, it is impossible to know whether it arrives safely and completely. UDP is used to support network applications that need to transfer data between computers. Many network applications in client/server mode, including network video conferencing systems, need to use the UDP protocol. The UDP protocol has been used for many years since its inception. Although its initial glory has been overshadowed by some similar protocols, UDP is still a very practical and feasible network transport layer protocol even today. 
Like the well-known TCP (Transmission Control Protocol) protocol, the UDP protocol sits directly on top of the IP (Internet Protocol) protocol. According to the OSI (Open Systems Interconnection) reference model, both UDP and TCP are transport layer protocols. The main function of the UDP protocol is to compress network data traffic into the form of data packets. A typical data packet is a transmission unit of binary data. The first 8 bytes of each data packet are used to contain header information, and the remaining bytes are used to contain specific transmission data.

Later we will talk about TCP, all of which can transmit data, but each has its own characteristics.

Ok, let's implement the UDP receiver first:

public class Save {

    public  static  void main(String[] args) throws Exception {
         // Define a receiver and specify the port number to receive 
        DatagramSocket socket = new DatagramSocket(8080 );

        while ( true ) {
             byte [] buf = new  byte [1024 ];
             // Parse the data packet 
            DatagramPacket packet = new DatagramPacket(buf, buf.length);

            socket.receive(packet);

            String ip = packet.getAddress().getHostAddress();

            buf = packet.getData();

            String data = new String(buf, 0, packet.getLength());

            System.out.println( "Received a message from " + ip + ": " + data);

        }

    }
}

In fact, it is not difficult, the code is just a few lines, and the data is printed out.

Then look at the sender:

public class Send {

    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket();
        byte[] buf = "ge men lai le".getBytes();
        //将数据打包
        DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.111.206"), 8080);
        socket.send(packet);
        socket.close();

    }

}

192.168.111.206 is your own local ip. How to get it: create a new java class, and then get it in the main method. The specific code is:

public class Mexception {
    public static void main(String[] args) throws UnknownHostException {
        System.out.println(InetAddress.getLocalHost().toString());
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325478423&siteId=291194637