TCP Connection Establishment
What is TCP Connection Establishment?
TCP uses a three-way handshake process to establish a reliable connection between two
devices before data transfer begins. This ensures both sides are ready and have agreed on
initial parameters like sequence numbers.
Steps in the Three-Way Handshake
1. SYN (Synchronize):
The client sends a TCP segment with the SYN flag set to the server.
This segment includes the client’s initial sequence number.
Purpose: "I want to start a connection and here’s my sequence number."
2. SYN-ACK (Synchronize-Acknowledge):
The server responds with a segment that has both SYN and ACK flags set.
The server acknowledges the client’s sequence number and sends its own initial
sequence number.
Purpose: "I received your request, here’s my sequence number, and I acknowledge
yours."
3. ACK (Acknowledge):
The client sends a segment with the ACK flag set, acknowledging the server’s sequence
number.
After this, the connection is established, and data can flow in both directions.
Visual Representation
Step Sender Receiver Flags Sent Purpose
1 (SYN) Client Server SYN Start connection, send initial sequence
2 (SYN-ACK) Server Client SYN, ACK Acknowledge SYN, send own sequence
3 (ACK) Client Server ACK Acknowledge server’s sequence
Handling Simultaneous Connection Attempts
If both hosts try to connect at the same time, both send SYN segments. Each responds to the
other’s SYN with SYN-ACK, and only one connection is established between the two endpoints.
Security Note: SYN Flood Attack
Attackers can abuse the handshake by sending many SYN requests without completing the
handshake, tying up server resources. To defend against this, servers may use SYN cookies,
which allow the server to avoid allocating resources until the handshake is completed.
TCP Connection Release
How is a TCP Connection Closed?
TCP allows either side to initiate connection termination. The process ensures all data is
transmitted and acknowledged before closing.
Steps to Release a Connection
1. FIN (Finish):
The side that wants to close sends a segment with the FIN flag set.
This means "I have no more data to send."
2. ACK (Acknowledge):
The other side acknowledges the FIN by sending an ACK.
3. (Optional) FIN from Other Side:
The other side may also send a FIN when it is ready to close its end.
The original side then sends an ACK to acknowledge this FIN.
Typically, four segments are exchanged: FIN, ACK, FIN, ACK.
Both directions are closed independently, so one side can finish sending while still receiving
data.
State Transitions
TCP uses a finite state machine to track connection states (e.g., ESTABLISHED, FIN WAIT,
TIME WAIT, CLOSED).
After both sides have exchanged FIN and ACK, the connection is fully closed.
Use of Timers
After closing, TCP waits for a period (TIME WAIT state) to ensure all packets are cleared
from the network before the port is reused.
Summary:
TCP uses a careful handshake to establish connections and a structured process to close them,
ensuring reliable communication and graceful connection termination. These steps prevent data
loss, ensure both sides are synchronized, and protect against certain types of attacks [1] .
⁂
1. Unit-5-notes.pdf