TCP Connection Establishment and Termination
TCPThree-Way HandshakePortsProcessesTCP Control Flags
TCP (Transmission Control Protocol) is a connection-oriented protocol that ensures reliable data transfer over networks. This guide covers the essentials of establishing and terminating TCP connections, including the three-way handshake process, the role of ports and processes, and key TCP control flags.
Key Points
- TCP is a connection-oriented protocol ensuring reliable data delivery.
- Ports and processes are essential for TCP communication.
- The three-way handshake establishes a TCP connection.
- Proper termination of a TCP connection involves a four-step process.
- TCP control flags (SYN, ACK, FIN, RST) manage connection states.
TCP and Ports
TCP Basics
- TCP (Transmission Control Protocol) is a connection-oriented transport-layer protocol that ensures reliable, ordered, and error-checked delivery of data.
- A connection-oriented protocol requires explicit establishment and termination of communication sessions.
Ports and Processes
- Port number: A numerical identifier (0–65535) used to associate network traffic with a specific process or service on a machine.
- Process: A running program that sends or receives data over the network using a port.
Two services cannot listen on the same port on the same IP address simultaneously.
Example
- Web browser uses a local ephemeral port (e.g.,
52344). - Web server listens on port
80 (HTTP)or443 (HTTPS).
Three-Way Handshake
Why a Handshake is Necessary
TCP must guarantee:
- Both sides are reachable.
- Both sides are ready to send and receive data.
- Sequence numbers are synchronized.
Steps of the Three-Way Handshake
Step 1 — SYN (Synchronize)
- Client sends a
SYNpacket to the server. - Meaning: "I want to establish a TCP connection and send data."
Step 2 — SYN + ACK
- Server replies with
SYN + ACK. - Meaning: "I acknowledge your request and I am ready to send data too."
Step 3 — ACK
- Client sends an
ACK. - Meaning: "Acknowledged. You can now send data."
At this point, the TCP connection is established.
ASCII Diagram
Client (A) Server (B)
| |
| ----------- SYN ----------------> |
| |
| <------- SYN + ACK -------------- |
| |
| ----------- ACK ----------------> |
| |
| Connection Established |
TCP Connection Termination
Four Steps to Terminate a Connection
- One side sends
FIN→ "I have no more data to send." - Other side sends
ACK. - Other side sends its own
FIN. - First side sends
ACK.
Termination Diagram
Host A Host B
| |
| ----------- FIN ----------------> |
| <----------- ACK ---------------- |
| <----------- FIN ---------------- |
| ----------- ACK ----------------> |
| |
| Connection Closed |
TCP Control Flags
| Flag | Meaning |
|---|---|
| SYN | Synchronize sequence numbers (start connection) |
| ACK | Acknowledgment of received data |
| FIN | Graceful connection termination |
| RST | Immediate connection reset |
| PSH | Push data immediately to application |
| URG | Urgent data |
Common Mistakes
- Confusing
SYNwithACKroles. - Thinking TCP setup is two steps instead of three.
- Assuming connection termination also uses three steps (it uses four).
- Believing ports identify machines (they identify processes).
- Forgetting that TCP is bidirectional by design.
Practical Example
Home Network Scenario
- Your laptop opens a website.
- Browser uses an ephemeral port (e.g.,
51432). - Web server listens on port
443. - TCP handshake establishes a reliable channel.
- Data is exchanged.
- Connection is closed using FIN/ACK sequence.
Key Takeaways
- TCP is a connection-oriented protocol.
- Each network process uses a port number.
- TCP setup uses a three-way handshake:
SYN → SYN/ACK → ACK. - TCP termination uses four steps.
FINindicates no more data to send.ACKconfirms receipt.- TCP flags control connection state.