Introduction of UDP

UDP: User Datagram Protocol.

It provides a way for applications to send encapsulated IP packets without establishing a connection.

UDP is characterized by connectionless, unreliable, and fast transmission.

UDP, like TCP, is used to process packets, and in the OSI model, both reside at the transport layer, one layer above IP. UDP does not provide packet grouping, assembly, and sorting. That is, after a packet is sent, it is impossible to know whether the packet arrived safely and intact. UDP is used to support network applications that need to transfer data between computers.

In general, audio, video, and ordinary data use UDP more for transmission, because the occasional loss of a packet or two does not have a big impact on the received results. For example, ICQ and QQ that we chat with are UDP protocols.

How does UDP send and receive data

UDP is a reliable network protocol. It establishes a Socket object at both ends of the communication protocol, but the two indexes send and receive the object. Therefore, there is no such concept as client and server for udP-based communication.

Java provides the DatagramSocket class as a UDP-based Socket.

UDP sending data

UDP Sending Procedure

1. Create the Socket object (DatagramSocket) on the sender.

2. Create data and package it;

3. Call the DatagramSocket object method to send data;

4. Close the sending terminal;

Fixed input sending UDP
Public static void main(String[] args) throws IOException {// Create a Socket object on the sender (DatagramSocket) DatagramSocket ds = new DatagramSocket(); // Create data and package it; String STR = "Hello,Java, I'm coming "; DatagramPacket dp = new DatagramPacket(str.getBytes(),str.getBytes().length, InetAddress. GetByName (" 172.16.47.101 "), 10001); //️ calls the DatagramSocket object's method to send data; ds.send(dp); // Close the sender; ds.close(); }Copy the code
Manually enter to send UDP

Further optimizations can be made to change writing-dead input to manual input.

Public static void main(String[] args) throws IOException {// Create the Socket object on the sender DatagramSocket ds = new DatagramSocket(); Br = new InputStreamReader(system.in); String line; while ((line = br.readLine()) ! = null){ if ("88".equals(line)){ break; Byte [] bytes = line.getbytes (); DatagramPacket dp = new DatagramPacket(bytes,bytes.length, inetaddress.getByName ("172.16.47.101"),12001); ds.send(dp); } // Close the data source ds.close(); }Copy the code
The code runs the output

UDP receiving data

Receiving steps

1. Create a Socket object (DatagramSocket) on the receiving end;

2. Create a packet to receive data.

3. Call DatagramSocket to receive data;

4. Parse data packets and display data documents on the console;

5. Close the receiving end;

Receives UDP data
Public static void main(String[] args) throws IOException {// Create a Socket object (DatagramSocket) on the receiving end; DatagramSocket ds = new DatagramSocket(12001); // Create a packet to receive data; byte[] bytes = new byte[1024]; DatagramPacket dp = new DatagramPacket(bytes,bytes.length); // Call the DatagramSocket method to receive data; ds.receive(dp); byte[] datas = dp.getData(); int length = dp.getLength(); String dataStr = new String(datas,0,length); System.out.println("udp received data :" +dataStr); ds.close(); }Copy the code
The code runs the output