While UDP is fast and unreliable, the vast majority of Internet traffic (Web browsing, Email, File Transfers) requires absolute perfection. If a single byte is lost while downloading a massive executable file or a bank statement, the entire file is corrupted and useless.
To guarantee perfect delivery over an inherently unreliable network (the Internet), the Transmission Control Protocol (TCP) is used.
1. Features of TCP
TCP provides a massive amount of functionality over UDP:
Connection-Oriented: Before any data is sent, TCP establishes a strict, stateful connection between the client and the server.
Reliable Delivery: TCP guarantees that every single byte sent will arrive. If a router drops a packet, TCP detects the loss and automatically retransmits it.
In-Order Delivery: IP packets can take different routes across the globe and arrive out of order. TCP assigns a Sequence Number to every packet and reassembles them in the exact correct order before passing them to the application.
Flow Control: If a supercomputer is sending data to a 10-year-old smartphone, the smartphone's memory buffer might overflow. TCP allows the receiver to tell the sender exactly how much data it can handle at any given moment (the Receive Window), preventing the sender from overwhelming the receiver.
2. The TCP Header
Because of all these features, the TCP header is large (minimum 20 bytes). Key fields include:
Source & Destination Port: Identifies the specific applications.
Sequence Number: Marks the byte offset of the data in this packet relative to the entire stream.
Acknowledgment Number: Tells the sender which bytes the receiver has successfully received so far.
Flags (Control Bits): 6 specific bits used to manage the connection state. The most important are SYN (Synchronize), ACK (Acknowledgment), FIN (Finish), and RST (Reset).
3. The 3-Way Handshake
Before a client and server can exchange a single byte of application data (like an HTTP request), they must establish a TCP connection. This is done via the 3-Way Handshake.
The purpose of the handshake is to synchronize the Sequence Numbers and exchange the Initial Window Sizes between both machines.
Step 1: SYN (Client $\to$ Server)
The client wants to establish a connection. It generates a random Initial Sequence Number (e.g., $X = 1000$) and sends a TCP segment with the SYN flag set to 1.
Meaning: "Hello server, I want to connect. My starting sequence number is 1000."
Step 2: SYN-ACK (Server $\to$ Client)
The server receives the request. It allocates memory buffers for the connection. It generates its own random Initial Sequence Number (e.g., $Y = 5000$). It sends back a segment with both the SYN and ACK flags set to 1. The Acknowledgment number is set to $X + 1$ (1001).
Meaning: "Hello client, I acknowledge your sequence 1000 (I am now expecting byte 1001). I also want to connect. My starting sequence number is 5000."
Step 3: ACK (Client $\to$ Server)
The client receives the SYN-ACK. It allocates its memory buffers. It sends a final segment with the ACK flag set to 1, acknowledging the server's sequence number by setting the ACK field to $Y + 1$ (5001).
Meaning: "I acknowledge your sequence 5000 (I am now expecting byte 5001). We are officially connected."
Once the 3-Way Handshake is complete, the connection is ESTABLISHED, and the client can finally send the HTTP GET request.
4. Connection Termination (4-Way Teardown)
When the application is done, the connection must be closed gracefully to ensure no data in transit is lost.
The Client sends a FIN segment (I have no more data to send).
The Server replies with an ACK (I acknowledge you are done). The server might still have data to send back, so the connection remains half-open.
When the Server is finished sending its final data, it sends its own FIN segment.
The Client replies with an ACK. The connection is officially closed.