UDP Baochang Advice _ Learning Notes

Source: Internet
Author: User
In the UDP programming, we are most likely to think of the problem is how many bytes to send a good?
Of course, this does not have the only answer, compared to different systems, different requirements, the answer is not the same, I am here only for
Like the ICQ type of sending chat messages to analyze the situation, for other situations, you may also be able to get a little help:
First of all, we know that TCP/IP is usually considered to be a four-layer protocol system, including link layer, network layer, Transport layer, application layer.
UDP belongs to the transport layer, below we look from the bottom to the previous step:
The length of the Ethernet (Ethernet) data frame must be between 46-1500 bytes, which is determined by the physical properties of the Ethernet.
This 1500-byte is called the MTU of the link layer (the Maximum transmission unit).
This does not mean that the length of the link layer is limited to 1500 bytes, in fact this MTU refers to the data area of the link layer.
Does not include 18 bytes of the header and tail of the link layer.
So, in fact, this 1500 byte is the length limit of the network layer IP datagram.
Because the header of the IP datagram is 20 bytes, the IP datagram has a maximum data size of 1480 bytes.
And this 1480 byte is used to put TCP message from the TCP segment or UDP datagram sent.
Because of the first 8 bytes of the UDP datagram, the maximum length of the UDP datagram's data area is 1472 bytes.
This 1472 byte is the number of bytes we can use.

What happens when the UDP data we send is greater than 1472.
This means that the IP datagram is greater than 1500 bytes, greater than the MTU. This time the sender IP layer needs to be fragmented (fragmentation).
Divide the datagram into slices so that each piece is less than the MTU. The receiver IP layer needs to reorganize the datagram.
This will do a lot of things, and more seriously, because of the characteristics of UDP, when a piece of data transmission lost, easy to receive
Unable to reorganize datagram. Will cause the entire UDP datagram to be discarded.

Therefore, in the ordinary LAN environment, I recommend that the data of UDP control to 1472 bytes below as well.

It is different when you are programming on the Internet, because routers on the Internet may set the MTU to a different value.
If we assume that the MTU is sending data for 1500来, and that the MTU value of a network is less than 1500 bytes, the system will use a series of machines
System to adjust the MTU value, so that the datagram can get to the destination smoothly, so many unnecessary operations.

Because the standard MTU value on the Internet is 576 bytes, I recommend that you do UDP programming on the Internet.
It is best to have the data length control for UDP within 548 bytes (576-8-20).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.