End-to-End Communication
The Transport Layer is responsible for end-to-end communication between applications. While the Network Layer gets packets from one device to another, the Transport Layer ensures that data arrives correctly, in order, and without errors โ or at least tells the application when it doesn't.
This is where the concepts of ports, segments, and reliability come into play.
Why Do We Need a Transport Layer?
The Network Layer delivers packets to the right device, but not to the right application. A computer might be running a web server, email server, and file server simultaneously. When a packet arrives, how does the OS know which application should receive it?
The answer is port numbers. Every application listens on specific ports. When data arrives at port 80, it goes to the web server. Port 25? Email server. Port 21? FTP server.
Ports and Sockets
A port is a 16-bit number (0-65535) that identifies a specific application or service on a device. A socket is the combination of an IP address and a port number โ it uniquely identifies a communication endpoint.
Socket = IP Address + Port
Example: 192.168.1.5:80
Well-known ports (0-1023):
โโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโ
โ Port โ Service โ
โโโโโโโโผโโโโโโโโโโโโโโโโโโโโโค
โ 20 โ FTP Data โ
โ 21 โ FTP Control โ
โ 22 โ SSH โ
โ 23 โ Telnet โ
โ 25 โ SMTP โ
โ 53 โ DNS โ
โ 80 โ HTTP โ
โ 443 โ HTTPS โ
โโโโโโโโดโโโโโโโโโโโโโโโโโโโโโ
Registered ports (1024-49151):
Used by specific applications (MySQL=3306, PostgreSQL=5432)
Dynamic/Private ports (49152-65535):
Temporarily assigned to client applications
Segmentation and Reassembly
The Transport Layer breaks large messages into smaller chunks called segments. Each segment gets a sequence number so the receiver can reassemble them in the correct order. If a segment is lost, only that segment needs to be retransmitted โ not the entire message.
Large message (3000 bytes)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Original Data โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Segmented (MTU = 1000 bytes):
โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ
โ Seq #1 โ โ Seq #2 โ โ Seq #3 โ
โ 1000 B โ โ 1000 B โ โ 1000 B โ
โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ
Two Flavors: TCP and UDP
The Transport Layer has two main protocols, each designed for different needs:
- TCP (Transmission Control Protocol) โ Reliable, ordered delivery. Guarantees that all data arrives correctly and in order. Slower but trustworthy. Used for web browsing, email, file transfer.
- UDP (User Datagram Protocol) โ Fast, lightweight, no guarantees. Data might arrive out of order, be duplicated, or not arrive at all. Used for video streaming, gaming, DNS queries.
The choice between TCP and UDP depends on what you value more: reliability or speed. We'll explore both in detail in the next lessons.