UDP is a very practical protocol. It has been around for decades since its birth, so we can see how important it is. If you want to master the essence of the network protocol, you cannot ignore the learning and attention of this Protocol. Now let's take a look at its main content. Like the well-known TCP transmission control protocol, UDP is directly located at the top layer of the IP Internet protocol. According to OSI Open System Interconnection) reference model, both UDP and TCP are transport layer protocols.
The main function of UDP protocol is to compress network data traffic into a datagram. A typical datagram is the unit of transmission of binary data. The first eight bytes of each datagram are used to contain header information, and the remaining bytes are used to contain specific transmitted data.
UDP Header
The UDP header consists of four fields, each of which occupies two bytes, as follows:
Source Port Number
Destination Port Number
Datagram Length
Check Value
The UDP protocol uses the port number to reserve its own data transmission channels for different applications. The UDP and TCP Protocols use this mechanism to support sending and receiving data simultaneously for multiple applications at the same time. The data sender can be a client or server) sends UDP data through the source port, while the data receiver receives data through the target port. Some network applications can only use static ports reserved or registered for them in advance, while other network applications can use unregistered dynamic ports. Because the UDP header uses two bytes to store the port number, the valid range of the port number is from 0 to 65535. Generally, a port number larger than 49151 represents a dynamic port.
The length of a datagram refers to the total number of bytes, including the header and data. Because the header length is fixed, this field is mainly used to calculate the variable-length data part, also known as the data load ). The maximum length of a datagram varies depending on the operating environment. Theoretically, the maximum length of a datagram containing a header is 65535 bytes. However, in some practical applications, the size of data packets is often limited, and sometimes reduced to 8192 bytes.
UDP uses the checksum value in the header to ensure data security. The verification value is calculated by the Data sender using a special algorithm. After it is passed to the receiver, it needs to be re-calculated. If a data packet is tampered with by a third party during transmission or damaged due to line noise or other reasons, the sent data does not match the verification calculation value of the receiver. Therefore, the UDP protocol can detect whether an error occurs. This is different from the TCP protocol, which requires a verification value.
UDP vs. TCP
The main difference between UDP and TCP is that they differ in how to implement reliable information transmission. The TCP protocol includes a special transmission guarantee mechanism. When the data receiver receives the message from the sender, it will automatically send a confirmation message to the sender; the sender continues to transmit other information only after receiving the confirmation message. Otherwise, the sender waits until the confirmation message is received.
Unlike TCP, UDP does not provide a data transmission guarantee mechanism. If a data packet is lost during transmission from the sender to the receiver, the Protocol itself cannot detect or prompt. Therefore, UDP is often called an unreliable transmission protocol.
Compared with the TCP protocol, another difference between the UDP protocol is how to receive multiple burst data packets. Unlike TCP, UDP does not guarantee the order in which data is sent and received. For example, an application on a client sends the following four data packets to the server.
D1
D22
D333
D4444
However, UDP may submit the received data to the application on the server in the following order:
D333
D1
D4444
D22
In fact, this out-of-order nature of UDP is rarely seen. Generally, it can only happen when the network is very crowded.
Application of UDP protocol
Some readers may ask, since UDP is an unreliable network protocol, what is the value or necessity? In fact, in some cases, UDP may become very useful. Because UDP has a speed advantage beyond the reach of TCP. Although various security protection functions are embedded in the TCP protocol, a large amount of system overhead will be occupied during actual execution, and the speed will undoubtedly be seriously affected. In contrast, UDP eliminates the information reliable transfer mechanism and transfers security and sorting functions to upper-layer applications, greatly reducing the execution time and ensuring the speed.
The earliest specification for UDP protocol was RFC768, which was released in 1980. Although it has been a long time, UDP continues to play a role in mainstream applications. Many applications, including video teleconference systems, prove the value of UDP. Compared with reliability, these applications focus more on actual performance. Therefore, in order to achieve better performance, for example, higher image frame refresh rate, certain reliability such as meeting quality can be sacrificed ). This is the trade-off between UDP and TCP. Based on different environments and features, the two transmission protocols will play a more important role in the future online world.