为什么UDP头部有长度字段而TCP没有呢?
我猜想,TCP段的长度可以由IP头部推断出来,但是在UDP数据报中也应该能够做到同样的事情。
为什么UDP头部有长度字段而TCP没有呢?
我猜想,TCP段的长度可以由IP头部推断出来,但是在UDP数据报中也应该能够做到同样的事情。
在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层级别上不需要该信息。
这只是UDP最初规定的方式。UDP可以像TCP一样在头部没有长度字段的情况下工作。
TCP中每个段的长度如何获得?这实际上取决于具体的实现(操作系统)。 TCP的RFC仅说明:
任何较低级别的协议都必须提供源地址、目标地址和协议字段,以及某种确定“TCP长度”的方法,[...]