views:

1521

answers:

4

Why does UDP have a length field in the header and TCP does not?

I am guessing that the length of the segment in TCP is inferred from the IP header but one should be able to do the same for a UDP datagram

+4  A: 

There is a 96 bit pseudo header conceptually prefixed to the TCP header that contains the information already.

The checksum field description from this source gives the answer:

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   |
      +--------+--------+--------+--------+

The information is not needed at the TCP level since TCP is a stream based protocol.

Brian R. Bondy
UDP uses the same pseudo header; see section 3.2 of this http://www.ietf.org/rfc/rfc3828.txt
mbyrne215
right since it is part of IP
Brian R. Bondy
+1  A: 

TCP is a stream based protocol, so in the end when the data comes out (e.g. you read from a socket), you simply read from a stream.

UDP is datagram based, here you put chunks of data in a pipe, and a read from the socket on the other side brings the same chunks out.

So rather intuitively you can say that on a udp level you need to know the length of the chunks that were pushed in there while on a tcp level you don't need to.

amo-ej1
-1: It is true that the UDP data length needs to be passed up to the application layer, unlike TCP. However, the TCP implementation still needs to know how many bytes to append to the stream receive buffer. It gets this length from the IP layer (which provides it for both TCP and UDP), so the UDP length field is unnecessary.
mark4o
+2  A: 

According to TCP/IP Illustrated Volume 1, the length field is redundant. That's all Stevens says on the matter.

I personally believe it was to make the UDP header length (in bits) divisible by 32 :)

jdizzle
He also adds (in volume 2) "Why does the UDP length field exist? Possibly to add a small amount of error checking, since UDP checksums are optional." However I agree that 32-bit alignment was probably part of the reason.
mark4o
+1  A: 

The length of the UDP header is fixed, unlike the length of the TCP header. From wikipedia:

The UDP header consists of 4 fields, all of which are 2 bytes (16 bits).

or 64 bits total!

Getimoliver