我读过许多关于UDP数据包大小的文章,但一直无法得出正确的结论。
许多服务将最大的UDP数据包限制在512字节(如dns)
假定internet上的最小MTU是576,IPv4报头的大小是20字节,UDP报头的大小是8字节。这就为用户数据留下了548个字节可用
我是否能够使用548大小的数据包而不产生数据包碎片?或者DNS的创造者知道一些事情,这就是为什么他们把它限制在512字节。
我能安全的把数据设置在548字节以上吗?
我读过许多关于UDP数据包大小的文章,但一直无法得出正确的结论。
许多服务将最大的UDP数据包限制在512字节(如dns)
假定internet上的最小MTU是576,IPv4报头的大小是20字节,UDP报头的大小是8字节。这就为用户数据留下了548个字节可用
我是否能够使用548大小的数据包而不产生数据包碎片?或者DNS的创造者知道一些事情,这就是为什么他们把它限制在512字节。
我能安全的把数据设置在548字节以上吗?
当前回答
It is true that a typical IPv4 header is 20 bytes, and the UDP header is 8 bytes. However it is possible to include IP options which can increase the size of the IP header to as much as 60 bytes. In addition, sometimes it is necessary for intermediate nodes to encapsulate datagrams inside of another protocol such as IPsec (used for VPNs and the like) in order to route the packet to its destination. So if you do not know the MTU on your particular network path, it is best to leave a reasonable margin for other header information that you may not have anticipated. A 512-byte UDP payload is generally considered to do that, although even that does not leave quite enough space for a maximum size IP header.
其他回答
512是您最好的选择。它在其他地方也被使用,是一个不错的偶数(1024的一半)。
UDP报文的最大大小的理论限制(Windows)是65507字节。这是记录在这里:
正确的UDP消息最大大小是65507,由以下公式确定: 0xffff -(sizeof(IP头)+ sizeof(UDP头))= 65535-(20+8)= 65507
也就是说,大多数协议限制的大小要小得多——通常是512或偶尔是8192。如果你在一个可靠的网络上,你通常可以安全地超过548,但如果你在整个互联网上广播,你的频率越大,你就越有可能遇到数据包传输问题和丢失。
It is true that a typical IPv4 header is 20 bytes, and the UDP header is 8 bytes. However it is possible to include IP options which can increase the size of the IP header to as much as 60 bytes. In addition, sometimes it is necessary for intermediate nodes to encapsulate datagrams inside of another protocol such as IPsec (used for VPNs and the like) in order to route the packet to its destination. So if you do not know the MTU on your particular network path, it is best to leave a reasonable margin for other header information that you may not have anticipated. A 512-byte UDP payload is generally considered to do that, although even that does not leave quite enough space for a maximum size IP header.
IPv4最小重组缓冲区大小为576,IPv6为1500。从这里减去标题大小。参见W. Richard Stevens的UNIX网络编程:)
I've read some good answers here; however, there are some minor mistakes. Some have answered that the Message Length field in the UDP header is a max of 65535 (0xFFFF); this is technically true. Some have answered that the actual maximum is (65535 - IPHL - UDPHL = 65507). The mistake, is that the Message Length field in the UDP Header includes all payload (Layers 5-7), plus the length of the UDP Header (8 Bytes). What this means is that if the message length field is 200 Bytes (0x00C8), the payload is actually 192 Bytes (0x00C0).
固定不变的是IP数据报的最大大小是65535字节。这个数字是L3和L4报头加上5-7层有效载荷的总和。IP头+ UDP头+层5-7 = 65535(最大)。
UDP数据报的最大大小最正确的答案是65515字节(0xFFEB),因为UDP数据报包括UDP头。UDP有效载荷最大大小的最正确答案是65507字节,因为UDP有效载荷不包括UDP头。