Sub topic 7
The transport layer: TCP, UDP, and ports
This chapter assumes you know what the internet layer does. It delivers packets between hosts by IP address. It also assumes you can run commands in a terminal. The transport layer sits above it. It takes delivery between two hosts and turns it into delivery between two programs.
What the transport layer adds
IP gets a packet to the right machine, but a machine runs many programs at once. The transport layer solves this with ports. A port is a 16-bit number (0 to 65535) that identifies one endpoint on a host. A connection is identified by the pair of IP addresses and the pair of ports, plus the protocol. This set is often called the 5-tuple.
In the OSI model this is layer 4. In the TCP/IP model it is the transport layer. The two main protocols are TCP and UDP.
| Feature | TCP | UDP |
|---|---|---|
| Connection setup | Yes, three-way handshake | None |
| Delivery guarantee | Retransmits lost data | None |
| Ordering | In order | Not guaranteed |
| Unit of data | Segment | Datagram |
| Typical use | HTTP, SSH, email | DNS queries, streaming, games |
Ports in practice
Servers listen on well-known ports. Examples are 80 for HTTP, 443 for HTTPS, 53 for DNS, and 22 for SSH. A client usually picks an ephemeral port, a temporary high-numbered port chosen by the operating system. So a browser talking to a web server might use 192.0.2.50:51514 to 192.0.2.10:80.
UDP: minimal and message-based
UDP adds only four header fields: source port, destination port, length, and checksum. It keeps message boundaries. Each send produces one datagram, which the receiver reads as one unit.
The program below supplies only the data, an address, and a UDP port. The operating system adds the UDP, IP, and link-layer headers.
import socket
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.sendto(b"hello", ("127.0.0.1", 9999))This runs without error even if nothing is listening on port 9999. UDP does not check that anyone received the data.
TCP: a reliable byte stream
TCP gives the application a reliable, ordered stream of bytes. It does not keep message boundaries. Two sends of 100 bytes might arrive as one read of 200 bytes. Applications must frame their own messages.
TCP achieves reliability with these mechanisms:
- Sequence numbers label every byte, so the receiver can reorder data and detect gaps.
- Acknowledgments tell the sender what arrived. Unacknowledged data is retransmitted.
- Flow control uses a receive window, so a fast sender does not overwhelm a slow receiver.
- Congestion control slows the sender when the network appears overloaded.
The three-way handshake
Before data flows, the two sides agree on starting sequence numbers.
You can build the first packet of this exchange yourself with Scapy. The / operator stacks headers, which mirrors encapsulation. This packet has the SYN flag set.
from scapy.all import Ether, IP, TCP
pkt = Ether() / IP(dst="192.0.2.10") / TCP(dport=80, flags="S")
pkt.show()The output lists the Ethernet, IP, and TCP layers in order. In the TCP section you will see dport = http and flags = S.
Seeing the transport layer in a capture
This command captures five TCP packets on port 80. The -e option prints link-layer addresses and -v prints IP and TCP header fields.
sudo tcpdump -nn -e -v -c 5 'tcp port 80'In the output, look for Flags [S], Flags [S.], and Flags [.]. These are the handshake. Also note the seq and ack values and the port numbers after each IP address, such as 192.0.2.50.51514 > 192.0.2.10.80.
Common errors
- Assuming UDP
sendtosuccess means delivery. It only means the OS accepted the data. - Assuming one TCP
sendmatches onerecv. TCP is a stream, so message boundaries are not preserved. - Confusing a refused connection with a timeout. A reset (RST) reply means the host is reachable but nothing listens on that port. A timeout usually means a firewall dropped the packet or the host is unreachable.
- Treating ports as unique across protocols. TCP port 53 and UDP port 53 are separate endpoints.
Exercise
A client connects to a web server and sees "connection refused" immediately. Which TCP behavior explains this, and what does it tell you about the network path?
Show answer
The server host answered with a TCP RST (reset) because no program was listening on that port. The reply shows that IP routing and the link layers work and the host is reachable. The fault is at the transport or application level, such as a stopped service or the wrong port.
Exercise
Your program calls send twice on a TCP socket, with 100 bytes each time. The receiver calls recv(1024) once and gets 200 bytes. Why?
Show answer
TCP is a byte stream and does not preserve message boundaries. The two sends were merged into one stream of bytes. The application must add its own framing, such as a length prefix or a delimiter.