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.
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:
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:
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
In the previous issue of k8s-Service Mesh Practic...
Just after Double Eleven, RackNerd released Black...
Netty is a high-performance, asynchronous event-d...
According to foreign media reports, the Open RAN ...
What is Cat6a Ethernet Cable? Cat6a cables repres...
The previous section introduced the evolution of ...
[[428404]] This article is reprinted from the WeC...
As we all know, the security of wireless routers ...
Netty version: 4.1.55.Final Traditional IO model ...
HostYun (host cloud, the original domain name hos...
With the vigorous development of cloud computing,...
Shuhost Technology has launched a year-end promot...
Olink.Cloud is said to be a site under the hostin...
In recent years, with the development of electron...
Enterprise network teams need to draft software-d...