I recently learned about UDP programming and got a big head. I have found a lot of materials and consulted many colleagues. I think the following information is well written, so I collected it for reference.
1. During UDP programming, the most common question is how many bytes are sent at a time?
Of course, there is no unique answer. The answer to this question varies with the requirements of different systems.
For example, if you want to analyze the situation of sending chat messages like ICQ, you may also get some help in other situations:
First, we know that TCP/IP is generally considered as a layer-4 protocol system, including the link layer, network layer, transport layer, and application layer.
UDP belongs to the transport layer. Let's look at it from the next step:
The length of an Ethernet data frame must be between-bytes, which is determined by the physical characteristics of the Ethernet.
This 1500 byte is called the MTU (maximum transmission unit) of the link layer ).
However, this does not mean that the link layer length is limited to 1500 bytes. In fact, this MTU refers to the data area of the link layer.
It does not include 18 bytes at the beginning and end of the link layer.
Therefore, in fact, this 1500 byte is the length limit of network layer IP datagram.
Because the IP datagram header is 20 bytes, the IP datagram data zone length is up to 1480 bytes.
The 1480 byte is used to store TCP packet segments or UDP datagram from UDP.
Because the first 8 bytes of the UDP datagram, the maximum length of the UDP datagram data zone is 1472 bytes.
This 1472 byte is the number of bytes that we can use. :)
What happens when we send UDP data greater than 1472?
This means that the IP datagram is greater than 1500 bytes and greater than MTU. At this time, the sender's IP layer needs to be split (fragmentation ).
Divide the datagram into several slices so that each slice is smaller than MTU, and the receiver IP layer needs to reorganize the datagram.
In this way, more things will be done, and more seriously, due to the characteristics of UDP, when a piece of data is lost during transmission, it is easy to receive
The datagram cannot be reorganized. This will cause the entire UDP datagram to be discarded.
Therefore, in a common LAN environment, it is recommended that UDP data be controlled below 1472 bytes.
When programming the Internet, the MTU may be set to different values on the internet router.
If we assume that the MTU is 1500 to send data, and the MTU value of a network passing through is smaller than 1500 bytes, the system will use a series of machines
To adjust the MTU value, so that the datagram can reach the destination smoothly, so that there will be many unnecessary operations.
Since the standard MTU value on the internet is 576 bytes, we recommend that you perform UDP programming on the Internet.
We recommend that you set the UDP data length to within 548 bytes (576-8-20.
2. What is the problem of UDP datagram overwrite and overlap?
Some brothers say that data overwrites and overlaps occur during UDP programming,
Overwrite indicates that the first message is sent as "first message", and the second message is "second message ".
The two received messages are the first one ".
When two messages are sent.
The first message received may be unfixed, such as "the first second message" and "the first second message.
This overlap is common in TCP programming.
However, I have never encountered these two situations in my programming experience.
In the LAN, I use an endless loop to continuously send UDP datagram to machine B.
However, the above two problems have never occurred.
Therefore, according to the characteristics of the UDP protocol, there will be no overlap problem like TCP based on byte stream connection.
Some brothers said that it was okay if he tried it on the LAN, but on the internet, he would. Is it true that the router combined the data,
Or are two messages arriving at the same time?
I think the latency of two messages will not be shorter than the latency in the LAN, right? Real-time, that probability is also very small.
In my opinion, even if the router restructured the route data, it also causes UDP datagram
When receiving, the checksum in the datagram is found to be inconsistent with the data, and the datagram is discarded.
Wheel learning Note 1: UDP programming in Ethernet
1. During UDP programming, the most common question is how many bytes are sent at a time?
Of course, there is no unique answer. The answer to this question varies with the requirements of different systems.
For example, if you want to analyze the situation of sending chat messages like ICQ, you may also get some help in other situations:
First, we know that TCP/IP is generally considered as a layer-4 protocol system, including the link layer, network layer, transport layer, and application layer.
UDP belongs to the transport layer. Let's look at it from the next step:
The length of an Ethernet data frame must be between-bytes, which is determined by the physical characteristics of the Ethernet.
This 1500 byte is called the MTU (maximum transmission unit) of the link layer ).
However, this does not mean that the link layer length is limited to 1500 bytes. In fact, this MTU refers to the data area of the link layer.
It does not include 18 bytes at the beginning and end of the link layer.
Therefore, in fact, this 1500 byte is the length limit of network layer IP datagram.
Because the IP datagram header is 20 bytes, the IP datagram data zone length is up to 1480 bytes.
The 1480 byte is used to store TCP packet segments or UDP datagram from UDP.
Because the first 8 bytes of the UDP datagram, the maximum length of the UDP datagram data zone is 1472 bytes.
This 1472 byte is the number of bytes that we can use. :)
What happens when we send UDP data greater than 1472?
This means that the IP datagram is greater than 1500 bytes and greater than MTU. At this time, the sender's IP layer needs to be split (fragmentation ).
Divide the datagram into several slices so that each slice is smaller than MTU, and the receiver IP layer needs to reorganize the datagram.
In this way, more things will be done, and more seriously, due to the characteristics of UDP, when a piece of data is lost during transmission, it is easy to receive
The datagram cannot be reorganized. This will cause the entire UDP datagram to be discarded.
Therefore, in a common LAN environment, it is recommended that UDP data be controlled below 1472 bytes.
When programming the Internet, the MTU may be set to different values on the internet router.
If we assume that the MTU is 1500 to send data, and the MTU value of a network passing through is smaller than 1500 bytes, the system will use a series of machines
To adjust the MTU value, so that the datagram can reach the destination smoothly, so that there will be many unnecessary operations.
Since the standard MTU value on the internet is 576 bytes, we recommend that you perform UDP programming on the Internet.
We recommend that you set the UDP data length to within 548 bytes (576-8-20.
2. What is the problem of UDP datagram overwrite and overlap?
Some brothers say that data overwrites and overlaps occur during UDP programming,
Overwrite indicates that the first message is sent as "first message", and the second message is "second message ".
The two received messages are the first one ".
When two messages are sent.
The first message received may be unfixed, such as "the first second message" and "the first second message.
This overlap is common in TCP programming.
However, I have never encountered these two situations in my programming experience.
In the LAN, I use an endless loop to continuously send UDP datagram to machine B.
However, the above two problems have never occurred.
Therefore, according to the characteristics of the UDP protocol, there will be no overlap problem like TCP based on byte stream connection.
Some brothers said that it was okay if he tried it on the LAN, but on the internet, he would. Is it true that the router combined the data,
Or are two messages arriving at the same time?
I think the latency of two messages will not be shorter than the latency in the LAN, right? Real-time, that probability is also very small.
In my opinion, even if the router restructured the route data, it also causes UDP datagram
When receiving, the checksum in the datagram is found to be inconsistent with the data, and the datagram is discarded.