为什么UDP头部有长度字段而TCP没有?

29

为什么UDP头部有长度字段而TCP没有呢?

我猜想,TCP段的长度可以由IP头部推断出来,但是在UDP数据报中也应该能够做到同样的事情。

3个回答

14
根据《TCP/IP 详解卷1》,长度字段是多余的。Stevens 在这个问题上只说了这些。
我个人认为这是为了使 UDP 头长度(以位为单位)能够被32整除 :)

3
他还在第2卷中补充道:“UDP长度字段的存在是为了什么?可能是为了增加少量的错误检查,因为UDP校验和是可选的。”然而我同意32位对齐可能也是其中的原因。 - mark4o
1
那是哪一章?您介意引用相关部分,以便其他用户也可以阅读吗? - Gerald
TCP/IP图解卷1第11.2节“UDP头”。 - jdizzle

1

在TCP头之前,概念上有一个96位的伪首部,其中包含已有的信息。

此来源中的校验和字段描述给出了答案:

Checksum: 16 bits

The checksum field is the 16 bit one's complement of the one's complement sum of all 16 bit words in the header and text. If a segment contains an odd number of header and text octets to be checksummed, the last octet is padded on the right with zeros to form a 16 bit word for checksum purposes. The pad is not transmitted as part of the segment. While computing the checksum, the checksum field itself is replaced with zeros.

The checksum also covers a 96 bit pseudo header conceptually prefixed to the TCP header. This pseudo header contains the Source Address, the Destination Address, the Protocol, and TCP length. This gives the TCP protection against misrouted segments. This information is carried in the Internet Protocol and is transferred across the TCP/Network interface in the arguments or results of calls by the TCP on the IP.

      +--------+--------+--------+--------+
      |           Source Address          |
      +--------+--------+--------+--------+
      |         Destination Address       |
      +--------+--------+--------+--------+
      |  zero  |  PTCL  |    TCP Length   |
      +--------+--------+--------+--------+

由于TCP是基于流的协议,因此在TCP层级别上不需要该信息。


7
UDP使用相同的伪头部,详情请参见http://www.ietf.org/rfc/rfc3828.txt的第3.2节。 - mbyrne215
这根本不是问题的答案。 - Remember Monica

0

这只是UDP最初规定的方式。UDP可以像TCP一样在头部没有长度字段的情况下工作。

TCP中每个段的长度如何获得?这实际上取决于具体的实现(操作系统)。 TCP的RFC仅说明:

任何较低级别的协议都必须提供源地址、目标地址和协议字段,以及某种确定“TCP长度”的方法,[...]


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接