UDP protocol - just read this article

UDP protocol - just read this article

Every programmer should know TCP and UDP protocols. UDP is the User Datagram Protocol, which belongs to the transport layer in the OSI model. It is a connectionless protocol, which means that there is no connection between the previous message and the next message at the protocol layer, and it provides a simple unreliable transmission service.

[[273925]]

That is to say, UDP is unreliable. If you want to make the data reliable, you need to do error correction and error detection at the service layer. For example: TFTP.

Then some students may ask, since it is unreliable, why not just use the IP protocol? Why go to so much trouble to add a protocol UDP?

In fact, one of the most important reasons is that there is no concept of port in the IP protocol. It only stipulates the communication between two hosts, and does not solve the communication between applications on different hosts. If multiple applications on a host need to communicate, the IP protocol cannot distinguish which application the data belongs to.

It can be understood that a port is a communication channel. Of course, UDP adds some functions based on the IP protocol, so let's summarize:

  1. UDP is connectionless, so the overhead of sending and receiving is much smaller.
  2. UDP does not guarantee reliable data delivery, but only tries its best to do so. Therefore, there is no need to maintain complex connection relationships.
  3. UDP is message-oriented, adding a data header at the application layer and directly giving it to the IP layer.
  4. UDP has no congestion control
  5. UDP supports multicast.
  6. The UDP header is small, indicating that more data is transmitted

The following figure shows the relationship between UDP and the upper and lower layers:

What does the UDP header look like?

First look at the following picture:

As can be seen from the figure, the UDP header consists of four parts:

  • The 16-bit source port and destination port are used to mark the sending and receiving application processes. Because UDP does not require a response, the source port is optional. If the source port is not used, it is set to zero. When the transport layer receives a UDP datagram from the IP layer, it passes the UDP datagram through the corresponding port according to the destination port in the header and submits it to the final destination - the application.
  • If the receiving UDP finds that the destination port number in the received message is incorrect, it will discard the message and send a "port unreachable" error message to the sender through the Internet Control Protocol ICMP. ICMP application Traceroute is to let the sent UDP user datagram deliberately use an illegal UDP port, and as a result, ICMP returns a "port unreachable" error message, thus achieving the purpose of the test.

Following the destination port is a fixed-length message length field in bytes, which is used to specify the length of the UDP datagram including the data portion. The minimum length is 8 bytes.

The remaining 16 bits of the header are used to check the header and data together. This part is optional, but this function is generally used in practical applications.

The checksums of UDP and TCP cover both their headers and data, while the checksum of the IP header only covers the IP header.

How to use UDP and socket together

As we enter the transport layer, we can also call the API in the operating system to build a socket. Socket is a programming interface provided by the operating system, which is used to represent a network communication. The application uses the socket to call the module that handles the network protocol in the system kernel, and these kernel modules are responsible for the implementation of the specific network protocol.

In this way, we can let the kernel receive the details of the network protocol, and we only need to provide the content to be transmitted. The kernel will help us control the format and further encapsulate it to the bottom layer. Therefore, in actual applications, we do not need to know how to form a UDP packet, but only need to provide relevant information (such as IP address, port number, and information to be transmitted). The operating system kernel will form a qualified UDP packet (as well as the lower layer packets and frames) based on the relevant information we provide before transmission. See the figure below.

UDP usage scenarios

It requires few resources, is used in an intranet with good network conditions, or is not sensitive to packet loss. For example, the DHCP protocol is based on UDP. Generally, IP addresses are obtained through intranet requests, and it is okay if the IP address cannot be obtained at once. For example, RTP and TFTP based on UDP, it is not a big problem to lose a frame of data. For example, some device discovery protocols, etc.

It does not require one-to-one communication to establish a connection, but can be broadcast. DHCP is a form of broadcast. VXLAN also requires multicast, which is also based on the UDP protocol.

It requires fast processing speed and low latency, and can tolerate a small amount of packet loss, but it also requires not to back down and to move forward even when the network is congested.

QUIC is a communication protocol proposed by Google based on improved UDP. Its purpose is to reduce the latency of network communication and provide a better user interaction experience.

Conclusion

UDP is not as complex as TCP, but it is an indispensable protocol in the network system. You need to master this protocol proficiently.

<<:  Huawei fully opens HMS and calls on more developers to join the new all-scenario smart ecosystem

>>:  New product pre-sale | Aruba Instant On MU-MIMO dual-band Gigabit Mesh wireless AP series products starting at 606 yuan

Recommend

South Korea's ICT exports hit record high in first half of the year

According to Yonhap News Agency, the Ministry of ...

Is the integrated air-space-ground information network really coming?

[[408522]] In recent years, with the popularizati...

Interpretation of the 2017 Global Data Breach Cost Research Report

In early July, the 2017 Global Cost of Data Breac...

DIY is not the best way to implement SD-WAN

Wide Area Networks (WANs) are not designed for th...

5G will catalyze the era of large-scale innovation in the whole society

Intuitively, 5G has a very obvious role in drivin...

Passive Wi-Fi technology bridges the digital divide

The digital divide is a term used to describe the...

Mobile phone + satellite, how difficult is it?

​These days, news about satellite phones has beco...

When WiFi6 collides with 5G, is it a crisis or a business opportunity?

September 16, 2019 WiFi Alliance announces WiFi6 ...

HostDare: 35% off VPS on CN2 GT line in Los Angeles, 10% off VPS on CN2 GIA line

HostDare has sorted out its VPS product inventory...