Sub topic 6
Transport layer: ports and the TCP or UDP header
This chapter assumes you know that IP addresses identify hosts and that a layer wraps the data it receives from above with its own header. It covers how the transport layer lets many programs on one host share a single IP address.
Why ports exist
An IP address gets a packet to the right host. The host may be running a browser, a mail client and a DNS resolver at once. The transport layer decides which program receives the data.
A port is a 16-bit number (0 to 65535) that identifies an endpoint on a host. A server listens on a well-known port, such as 80 for HTTP or 53 for DNS. A client gets a temporary source port, called an ephemeral port, from the operating system, for example 50000.
A connection is identified by four values: source IP, source port, destination IP and destination port. Two browser tabs to the same server differ only in their source ports.
The UDP header
UDP (User Datagram Protocol) is the simpler protocol. Its header is 8 bytes. UDP adds ports and an integrity check, and nothing else. It does not retransmit lost data, order data or open a connection.
| Field | Size | Purpose |
|---|---|---|
| Source port | 2 bytes | Sender's endpoint |
| Destination port | 2 bytes | Receiver's endpoint |
| Length | 2 bytes | Header plus data, in bytes |
| Checksum | 2 bytes | Detects corruption |
The TCP header
TCP (Transmission Control Protocol) provides a reliable, ordered byte stream. Its header is at least 20 bytes, and the extra fields support that reliability.
| Field | Size | Purpose |
|---|---|---|
| Source and destination port | 2 bytes each | Same role as in UDP |
| Sequence number | 4 bytes | Position of the first data byte in the stream |
| Acknowledgment number | 4 bytes | Next byte the sender expects to receive |
| Data offset | 4 bits | Header length in 32-bit words |
| Flags | 9 bits | SYN, ACK, FIN, RST, PSH and others |
| Window | 2 bytes | How much more data the receiver will accept |
| Checksum | 2 bytes | Detects corruption |
Data offset is a word count, so a value of 5 means 5 × 4 = 20 bytes. Options such as MSS make the header longer, and a parser must use the data offset to find where the payload starts.
Parsing both headers
This program builds one header of each kind and reads the fields back. All values use network byte order, which is big-endian, so the format string starts with !.
import struct
payload = b"hello"
# TCP: ports, seq, ack, offset+flags, window, checksum, urgent pointer
tcp = struct.pack("!HHIIHHHH", 50000, 80, 1, 0, (5 << 12) | 0x018, 65535, 0, 0)
sport, dport, seq, ack, off_flags, window, checksum, urgent = struct.unpack("!HHIIHHHH", tcp)
header_len = (off_flags >> 12) * 4
flags = off_flags & 0x1FF
print("TCP", sport, dport, "header bytes:", header_len, "flags:", hex(flags))
print("PSH set:", bool(flags & 0x008), "ACK set:", bool(flags & 0x010))
# UDP: ports, length, checksum
udp = struct.pack("!HHHH", 53000, 53, 8 + len(payload), 0) + payload
sport, dport, length, checksum = struct.unpack("!HHHH", udp[:8])
print("UDP", sport, dport, "length:", length, "data:", udp[8:].decode())The output shows a 20-byte TCP header with flags 0x18 (PSH and ACK set), then a UDP datagram of length 13 carrying hello.
Choosing between them
- Use TCP when every byte must arrive in order, as with web pages, file transfers and email.
- Use UDP when speed or low delay matters more than completeness, or when the application handles loss itself. DNS queries, voice calls and games are common examples.
Exercise
A TCP header has a data offset of 8. How many bytes long is the header, and how many of them are options?
Show answer
The header is 8 × 4 = 32 bytes long. The fixed header is 20 bytes, so 12 bytes are options.