When your talent is not enough to meet your ambition, you should calm down and study hard
Overview of UDP
-
UDP is the abbreviation of User Datagram Protocol and its Chinese name is User Datagram Protocol. It is a connectionless transport layer protocol in OSI (Open System Interconnection) reference model. It provides simple and unreliable information transmission service oriented to transaction. IETF RFC 768 is the formal specification of UDP. The protocol number of UDP in IP message is 17.
-
The full name of UDP protocol is user datagram protocol. In the network, it is used to process packets like TCP protocol. It is a connectionless protocol. In the OSI model, the transport layer, the fourth layer, is the upper layer of the IP protocol. UDP does not provide packet Grouping, assembly and sorting. That is to say, after the message 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 client / server mode network applications, including network video conference system, need to use UDP protocol. UDP protocol has been used for many years since it came out. Although its initial glory has been covered by some similar protocols, UDP is still a very practical and feasible network transport layer protocol even today.
Main features of UDP
-
UDP is a connectionless protocol. Before transmitting data, the source and terminal do not establish a connection. When it wants to transmit, it simply grabs the data from the application and throws it to the network as soon as possible. At the sending end, the speed of UDP data transmission is only limited by the speed of data generated by the application, the ability of the computer and the transmission bandwidth; At the receiving end, UDP puts each message segment in the queue, and the application reads one message segment from the queue at a time.
-
Since no connection is established for data transmission, there is no need to maintain the connection status, including sending and receiving status. Therefore, one server can transmit the same message to multiple clients at the same time.
-
The header of UDP packet is very short, only 8 bytes. Compared with the 20 byte packet of TCP, the additional overhead of UDP is very small.
-
The throughput is not regulated by the congestion control algorithm, but only limited by the data rate generated by the application software, transmission bandwidth, source and terminal host performance.
-
UDP is message oriented. The UDP of the sender delivers the message handed over by the application to the IP layer after adding the header. It neither splits nor merges, but retains the boundaries of these messages. Therefore, the application needs to select the appropriate message size.
Although UDP is an unreliable protocol, it is an ideal protocol for distributing information. For example, report the stock market on the screen, display aviation information, and so on. UDP is also used to modify the routing table in the Routing Information Protocol RIP (Routing Information Protocol). If there are new messages, it will replace them in another occasion in a few seconds. UDP is widely used in multimedia applications.
Difference between TCP and UDP
- TCP is a connection oriented transmission control protocol, while UDP provides connectionless datagram service;
- TCP has high reliability to ensure the correctness of transmitted data without loss or disorder. UDP does not establish a connection before transmitting data, does not check and modify datagrams, and does not need to wait for the other party's response. Therefore, packet loss, repetition and disorder will occur. The application program needs to be responsible for all work on transmission reliability;
- UDP has better real-time performance and higher work efficiency than TCP protocol;
- UDP segment structure is simpler than TCP segment structure, so the network overhead is also small;
- TCP protocol can ensure that the receiver can receive the byte stream sent by the sender without error, and provide reliable communication services for applications. Communication systems that require high reliability often use TCP to transmit data.
Main application
Applicable occasions
You must be careful when choosing UDP as the transport protocol. In the environment of unsatisfactory network quality, UDP protocol packet loss will be more serious. However, due to the characteristics of UDP: it does not belong to the connection protocol, so it has the advantages of low resource consumption and fast processing speed. Therefore, UDP is usually used more for audio, video and ordinary data transmission, because even if they occasionally lose one or two packets, they will not have a great impact on the receiving results. For example, ICQ and QQ used in our chat are UDP protocols.
practical application
In the field of field measurement and control, it is oriented to distributed controllers and monitors. Its application environment is relatively bad, so it puts forward different requirements for data transmission, such as real-time, anti-interference, security and so on. Based on this, in the field communication, if an application wants to transmit a group of data to another node in the network, the UDP process can add the header to the data and then transmit it to the IP process. The UDP protocol eliminates the process of establishing and dismantling the connection, cancels the retransmission inspection mechanism, and can achieve a higher communication rate.
Code demonstration
A simple example of client / server data sending and receiving
UDP client:
public class UdpClient { public static void main(String[] args) throws IOException { //Establish a socket DatagramSocket socket = new DatagramSocket(); //Create a packet String msg = "Hello, server~"; InetAddress localhost = InetAddress.getByName("localhost"); int port = 9090; DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port); //Send packet socket.send(packet); //Close flow socket.close(); } }
UDP server:
public class UdpServer { public static void main(String[] args) throws IOException { //Open port DatagramSocket socket = new DatagramSocket(9090); //Receive packet byte[] bytes = new byte[1024]; DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length); //Blocking reception socket.receive(packet); String msg = new String(packet.getData(),0,packet.getLength()); System.out.println("Data received from client:" + msg); //Close data flow socket.close(); } }
Simulate a chat conversation between a student and a teacher
//client public class TalkSend implements Runnable { DatagramSocket socket = null; BufferedReader br = null; private int formPort; private String toIp; private int toPort; public TalkSend(int formPort, String toIp, int toPort) { this.formPort = formPort; this.toIp = toIp; this.toPort = toPort; try { socket = new DatagramSocket(this.formPort); //Prepare data console to read system in br = new BufferedReader(new InputStreamReader(System.in)); } catch (SocketException e) { e.printStackTrace(); } } @Override public void run() { while (true) { try { String msg = br.readLine(); byte[] bytes = msg.getBytes(); DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, new InetSocketAddress(this.toIp, this.toPort)); //send data socket.send(packet); if (msg.equals("bye")) { break; } } catch (IOException e) { e.printStackTrace(); } } socket.close(); } }
//Server public class TalkReceive implements Runnable { DatagramSocket socket = null; private int port; private String msgForm; public TalkReceive(int port, String msgForm) { this.port = port; this.msgForm = msgForm; try { socket = new DatagramSocket(port); } catch (SocketException e) { e.printStackTrace(); } } @Override public void run() { while (true) { try { //Ready to receive package byte[] bytes = new byte[1024]; DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length); socket.receive(packet); //Disconnect bye String msg = new String(packet.getData(), 0, packet.getLength()); System.out.println(msgForm + ":" + msg); if ("bye".equals(msg)) { break; } } catch (IOException e) { e.printStackTrace(); } } socket.close(); } }
public class TalkStudent { public static void main(String[] args) { new Thread(new TalkSend(7777,"localhost",8888)).start(); new Thread(new TalkReceive(9999,"teacher")).start(); } }
public class TalkTeacher { public static void main(String[] args) { new Thread(new TalkSend(5555,"localhost",9999)).start(); new Thread(new TalkReceive(8888,"student")).start(); } }
The test results are as follows:
summary
-
UDP user datagram protocol is a connectionless communication protocol. UDP data includes destination port number and source port number information. Since communication does not require connection, broadcast transmission can be realized.
-
UDP communication does not need to be confirmed by the receiver. It is an unreliable transmission, and packet loss may occur. In practical application, programmers are required to program and verify.