UDP Packet Size Problem
Post from: http://blog.csdn.net/buptzwp/archive/2009/12/22/5055487.aspx
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 is different from the requirements of different systems. Here we only analyze the situation of sending chat messages like ICQ, in other cases, you may be able to get some help:
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 belong 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. in this case, fragmentation is required for the sender's IP layer ). divides the datagram into several slices so that each segment is smaller than MTU. the receiver's IP layer needs to reorganize the datagram. in this way, we will do more things. More seriously, due to the characteristics of UDP, when a piece of data is lost during transmission, it is easy to receive and cannot reorganize the datagram. the entire UDP datagram is 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 mechanisms to adjust the MTU value, enable the datagram to reach the destination smoothly, and then perform 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.
Theoretically, the maximum length of an IP datagram is 65535 bytes, which is limited by the 16-bit total length field of the IP header. Remove the 20-byte IP header and 8-byte UDP header. The maximum length of user data in UDP datagram is 65507 bytes. However, the length provided by most implementations is smaller than the maximum value.
We will encounter two restrictions. First, applications may be restricted by their program interfaces. The socket API provides a function that can be called by a program to set the length of the receiving and sending cache. For UDP socket, this length is directly related to the maximum UDP datagram length that the application can read and write. Most systems now provide UDP datagram that can read and write more than 8192 bytes by default (this default value is used because 8192 is the default value for NFS to read and write user data ).
The second restriction comes from the TCP/IP kernel implementation. There may be some implementation features (or errors), so that the IP datagram length is less than 65535 bytes.
In SunOS 4.1.3, the maximum IP datagram length of the loopback interface is 32767 bytes. Errors may occur if the value is greater than the value.
However, in the case of BSD/386 to SunOS 4.1.3, Sun can receive a maximum IP datagram of 32786 bytes (32758 bytes of user data ).
Use the loopback interface in Solaris 2.2. the maximum length of IP data packets that can be sent and received is 65535 bytes.
From Solaris 2.2 to AIX 3.2.2, the maximum length of IP datagram sent can be 65535 bytes. Obviously, this restriction is related to the implementation of the source and target terminals.
The host must be able to receive IP datagram with a minimum of 576 bytes. In the design of Many UDP applications, the application data is limited to 512 bytes or smaller, so it is smaller than this limit value.
Because the IP address can send or receive data of a specific length, it does not mean that the receiving application can read data of this length. Therefore, the UDP programming interface allows an application to specify the maximum number of bytes returned each time. What happens if the received datagram is longer than the length that the application can process? Unfortunately, the answer to this question depends on the programming interface and implementation.
A typical Berkeley socket API truncates data packets and discards any redundant data. The time when the application can know is related to the version (4.3BSD Reno and later versions can notify the application datagram to be truncated ).
The socket API (including Solaris 2.x) under SVR4 does not intercept the datagram. The excess data is returned in subsequent reads. It does not notify the application to read data from a single UDP datagram multiple times. TLI APIs do not discard data. Instead, it returns a flag indicating that more data can be obtained, and the read operation after the application will return the rest of the datagram. When discussing TCP, we found that it provides continuous byte streams for the application without any information boundaries. TCP transmits data according to the length required by the read operation of the application. Therefore, no data loss occurs in this interface.
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.
------------------------------------------------------------------- Reprint split line ------------------------------------------------------
In the LAN environment, the UDP packet size is 1024*8, and the speed is 2 Mb/s. The packet loss is ideal.
In the Internet environment, the UDP packet size is 548, the speed is ideal, and the packet loss is ideal.