Overview of TCP Connection Lifecycle
Before two devices can start sending data over TCP, they first need to establish a connection. This process is important because TCP is connection-oriented, meaning both sides must agree to communicate and synchronize their communication parameters.
The TCP connection lifecycle has three main phases:
- Connection Establishment – setting up the connection (the handshake).
- Data Transfer – actual sending and receiving of data.
- Connection Termination – closing the connection once communication ends.
This chapter focuses on Connection Establishment, which uses the famous Three-Way Handshake.
The Three-Way Handshake
The Three-Way Handshake is a three-step process where both the client and server synchronize and agree to communicate. It sets initial sequence number confirms both sides are ready.
Step 1: SYN (Synchronize)
- The client want to open a connection.
- It sends a TCP segment with the SYN flag set to 1.
- This segment contains the client's initial sequence number (ISN) – a random number used ton track bytes.
- The client is basically saying, “Hey, I want to connect, here's my starting point!”
Step 2: SYN-ACK (Synchronize-Acknowledge)
- The server receives the SYN.
- If ready, it sends back a TCP segment with both SYN and ACK flags set.
- This segment contains the server's own initial sequence number (its ISN).
- It also acknowledges the client's SYN by setting the acknowledgement number to the client's ISN + 1.
- Server says, “Got your SYN, here's mine, and I acknowledge yours.”
Step 3: ACK (Acknowledge)
- The client receives the SYN-ACK.
- It responds with a TCP segment with the ACK flag set.
- The acknowledgement number is set to the server's ISN + 1.
- Client says, “Got your SYN-ACK, connection established!”
After these three steps, the connection is fully established, and both sides have agreed on the initial sequence numbers. Now they can start sending actual data.
TCP Flags and Their Meaning
TCP segments contain special control bits called flags that control connection setup, data transfer, and termination. Here are the most important ones during connection establishment:
Flag | Full Name | Purpose |
---|---|---|
SYN | Synchronize | Used to initiate a connection |
ACK | Acknowledge | Acknowledges receipt of data or control packets |
FIN | Finish | Indicates sender wants to close connection |
RST | Reset | Abruptly terminates a connection |
PSH | Push | Pushes data to the receiving application immediately |
URG | Urgent | Marks urgent data |
During the handshake, SYN and ACK are used in combination to negotiate the connection.
Practical Example of the Handshake
Imagine you want to visit a website:
- Your computer (client) sends a SYN packet to the web server.
- The web server replies with a SYN-ACK.
- Your computer sends a ACK back.
Only after this handshake, the HTTP data (web page) starts flowing.
If any step fails (e.g., the server doesn't reply), the connection never establishes, and your browser shows an error.
Leave a comment
Your email address will not be published. Required fields are marked *