The Transport Layer (Layer 4) is responsible for process-to-process delivery—the delivery of a message from one specific application program on one computer to a specific application program on another computer.
While the Network Layer (IP) gets the packet to the correct destination computer, it is the Transport Layer's job to ensure the data gets to the correct application on that computer (e.g., your web browser instead of your email client).
1. Port Numbers
To identify the specific application, the Transport layer uses Port Numbers. A port is a 16-bit integer (ranging from 0 to 65535).
Well-Known Ports: 0 to 1023 (e.g., HTTP is port 80, HTTPS is 443, SSH is 22).
Registered Ports: 1024 to 49151.
Dynamic/Private Ports: 49152 to 65535 (used temporarily by client applications when initiating a connection).
When your browser connects to a web server, it might open dynamic port 50432 locally, and connect to port 80 on the remote server.
2. Transmission Control Protocol (TCP)
TCP is a connection-oriented, reliable protocol. It guarantees that the data sent will arrive intact, in the correct order, and without duplicates.
The Three-Way Handshake
Before sending any data, TCP establishes a connection using a three-way handshake:
SYN: Client sends a synchronization packet to the server to initiate the connection.
SYN-ACK: Server receives it, reserves resources, and sends back an acknowledgment.
ACK: Client receives the SYN-ACK, reserves resources, and sends a final acknowledgment. The connection is now established.
Reliability Mechanisms
Sequence Numbers: TCP breaks data into chunks called segments and assigns a sequence number to each. This allows the receiver to reassemble the data in the correct order, even if the packets arrive out of order.
Acknowledgments & Retransmissions: When the receiver gets a segment, it sends an ACK back to the sender. If the sender does not receive an ACK within a certain timeframe (timeout), it assumes the packet was lost in the network and automatically retransmits it.
Congestion Control: TCP constantly monitors the network. If it detects congestion (dropped packets), it automatically slows down its transmission rate to prevent overwhelming the routers.
3. User Datagram Protocol (UDP)
UDP is a connectionless, unreliable protocol. It does not establish a connection, does not guarantee delivery, does not guarantee order, and does not provide congestion control.
It simply takes the data, slaps a port number on it, and fires it into the network.
Why use UDP?
Because it doesn't have the overhead of handshakes, acknowledgments, or sequence numbers, UDP is blazing fast. It is used in applications where speed is more important than perfect reliability:
Live Video Streaming (Netflix/YouTube use TCP, but live Twitch/Zoom might use UDP).
Online Multiplayer Gaming (if you drop a packet containing your character's position from 2 seconds ago, you don't want it retransmitted; you only care about the current position).
DNS Queries (a tiny request that needs an instant answer).