TCP Connection Establishment and Termination
TCPThree-Way HandshakePortsProcessesTCP Control Flags
TCP (Transmission Control Protocol) is a connection-oriented protocol that ensures reliable, ordered, and error-checked data delivery over networks. It establishes connections using a three-way handshake and terminates them through a four-step process, with ports, processes, and control flags playing critical roles.
Key Points
- TCP guarantees reliable data delivery through explicit connection setup and teardown.
- Ports (0–65535) associate network traffic with specific processes or services.
- The three-way handshake (
SYN → SYN/ACK → ACK) synchronizes sequence numbers and confirms readiness. - Connection termination requires four steps (
FIN → ACK → FIN → ACK). - TCP control flags (
SYN,ACK,FIN,RST,PSH,URG) manage connection states.
TCP Basics
Connection-Oriented Protocol
- TCP operates at the transport layer (Layer 4 of the OSI model).
- Requires explicit establishment (handshake) and termination (FIN sequence) for each session.
Ports and Processes
- Port number: Numerical identifier (e.g.,
80for HTTP,443for HTTPS) linking traffic to a process. - Process: A running program (e.g., web server) that binds to a port to send/receive data.
Two services cannot listen on the same port/IP combination simultaneously.
Example:
- Client (e.g., browser) uses an ephemeral port (
52344). - Server listens on a well-known port (
80or443).
Three-Way Handshake
Purpose
Ensures both parties:
- Are reachable.
- Are ready to transmit/receive.
- Agree on initial sequence numbers.
Steps
- SYN
- Client → Server:
SYN("Initiate connection, here’s my sequence number").
- Client → Server:
- SYN + ACK
- Server → Client:
SYN/ACK("Acknowledged your SYN; here’s my sequence number").
- Server → Client:
- ACK
- Client → Server:
ACK("Acknowledged your SYN; connection established").
- Client → Server:
Diagram:
Client Server
| |
|------ SYN --------> |
| |
| <--- SYN + ACK ----|
| |
|------ ACK --------> |
| |
| Connection Open |
TCP Connection Termination
Four-Step Process
- FIN
- Host A → Host B:
FIN("I’m done sending data").
- Host A → Host B:
- ACK
- Host B → Host A:
ACK("Received your FIN").
- Host B → Host A:
- FIN
- Host B → Host A:
FIN("I’m also done").
- Host B → Host A:
- ACK
- Host A → Host B:
ACK("Connection closed").
- Host A → Host B:
Diagram:
Host A Host B
| |
|------ FIN --------> |
| <----- ACK --------|
| <----- FIN --------|
|------ ACK --------> |
| |
| Connection Closed |
Note: The side initiating
FINenters a TIME_WAIT state (2–4 minutes) to ensure all packets are delivered.
TCP Control Flags
| Flag | Purpose | Use Case |
|---|---|---|
SYN | Synchronize sequence numbers | Initiate connection |
ACK | Acknowledge received data | Confirm receipt of packets |
FIN | Graceful termination | Close connection |
RST | Reset connection immediately | Abort connection (e.g., errors) |
PSH | Push data to application | Prioritize urgent data |
URG | Mark data as urgent | High-priority transmission |
Common Misconceptions
- ❌ TCP uses a two-step handshake (it’s three steps).
- ❌ Termination is a three-step process (it’s four steps).
- ❌ Ports identify machines (they identify processes on a machine).
- ❌
SYNandACKare interchangeable (they serve distinct roles).
Practical Example: Web Browsing
- Your browser (client) assigns an ephemeral port (e.g.,
51432). - Server listens on port
443(HTTPS). - Handshake:
- Browser → Server:
SYN - Server → Browser:
SYN/ACK - Browser → Server:
ACK
- Browser → Server:
- Data exchange (e.g., loading a webpage).
- Termination:
- Browser → Server:
FIN - Server → Browser:
ACK - Server → Browser:
FIN - Browser → Server:
ACK
- Browser → Server:
Key Takeaways
- TCP ensures reliable, ordered data delivery.
- Ports map traffic to processes; ephemeral ports are temporary.
- Handshake:
SYN → SYN/ACK → ACK. - Termination:
FIN → ACK → FIN → ACK. - Control flags (
SYN,ACK,FIN, etc.) manage connection states.