Skip to main content

Command Palette

Search for a command to run...

TCP 3-Way Handshake

Updated
4 min read

What Is the TCP 3-Way Handshake?

Before any data is exchanged, TCP establishes a connection using a process called the 3-Way Handshake.

This ensures:

  • both devices are ready

  • both agree on initial sequence numbers

  • communication is synchronized

https://static.afteracademy.com/images/what-is-a-tcp-3-way-handshake-process-three-way-handshaking-establishing-connection-6a724e77ba96e241.jpg

https://static.afteracademy.com/images/what-is-a-tcp-3-way-handshake-process-three-way-handshaking-terminating-connection-6ea4a4c72d165361.jpg

https://www.ionos.com/digitalguide/fileadmin/DigitalGuide/Schaubilder/EN-tcp.png


5. Step-by-Step Working of SYN, SYN-ACK, and ACK

Let’s assume:

  • Client (A) wants to connect

  • Server (B) is listening

Step 1: SYN (Connection Request)

  • Client sends a packet with the SYN flag set

  • It includes an initial sequence number (ISN)

Meaning:

“I want to start a connection, and here’s my starting number.”


Step 2: SYN-ACK (Acknowledgment + Request)

  • Server responds with:

    • SYN (its own request)

    • ACK (acknowledging client’s sequence number + 1)

  • Server also sends its own initial sequence number

Meaning:

“I got your request. I’m ready too.”


Step 3: ACK (Final Confirmation)

  • Client sends an ACK

  • Acknowledges server’s sequence number + 1

  • No data is sent in this step

Meaning:

“Confirmed. Let’s start communication.”

✔️ Connection is now established
✔️ Data transfer begins


6. How Data Transfer Works in TCP

TCP treats data as a continuous stream, not individual messages.

TCP Segmentation

  • Large data is broken into smaller segments

  • Each segment is assigned a sequence number

Sequencing & Acknowledgment

  • Receiver uses sequence numbers to:

    • reassemble data in correct order

    • detect missing segments

  • Receiver sends ACKs confirming received data

If a segment is missing:

  • sender retransmits it

7. How TCP Ensures Reliability, Order, and Correctness

TCP achieves reliability using multiple mechanisms:

1. Sequence Numbers

Ensure correct ordering of data

2. Acknowledgments (ACKs)

Confirm successful delivery

3. Retransmission

Lost packets are resent automatically

4. Sliding Window (Flow Control)

Controls how much data can be sent before acknowledgment

5. Error Detection

Corrupted segments are discarded and resent

This is why TCP is slower than UDP — but far more reliable.


8. Graceful Termination of a TCP Connection

TCP connections do not end abruptly.
They are closed gracefully to avoid data loss.

https://www.tcpipguide.com/free/diagrams/tcpclose.png

https://media.licdn.com/dms/image/v2/D4D12AQE2jYQr3vK4Gw/article-cover_image-shrink_600_2000/article-cover_image-shrink_600_2000/0/1670059962890?e=2147483647&t=qC1aZZXh06W0eJLuo3rYaxSmieotg8JNIJXX3GIpEQQ&v=beta

https://totozhang.github.io/2016-01-31-tcp-timewait-status/TCPTimeWait1.png

Connection Termination Process

  1. One host sends FIN (Finish)

  2. Other host responds with ACK

  3. Second host sends its own FIN

  4. First host responds with ACK

  5. Connection enters TIME-WAIT

  6. Finally, both sides close the connection

This ensures:

  • all remaining data is delivered

  • duplicate packets are handled safely


9. What Is a Half-Open TCP Connection?

A TCP connection is half-open when:

  • one side believes the connection is active

  • the other side does not

This can happen due to:

  • network failure

  • firewall interruption

  • system crash

Half-open connections can waste server resources and are handled using timeouts and resets.


10. Simple Analogy: TCP Handshake in Real Life

Think of a phone call:

  1. You: “Can we talk?” → SYN

  2. Other person: “Yes, I’m ready. Are you?” → SYN-ACK

  3. You: “Yes.” → ACK

Conversation starts.
When both are done, they say goodbye properly — not by hanging up randomly.

That is TCP.


11. Summary

  • TCP provides reliable, ordered, and error-checked data transfer

  • The 3-Way Handshake establishes a safe connection

  • Sequence numbers and ACKs ensure correctness

  • Retransmission handles packet loss

  • FIN and ACK close connections gracefully

TCP is the reason the web works predictably and safely.

More Simplified Form

🔷 TCP 3-Way Handshake between Me & Hitesh Sir :

Step 1: Me : "Are you ready?" (SYN)

I sends Hitesh sir a WhatsApp text first:
📱 "Sir, can we video call now? I need to clarify something."
I am initiating the connection and checking if Sir is available.

TCP Equivalent: SYN packet - Client (Me) says "I want to connect"


Step 2: Hitesh Sir's "Yes, I'm ready! Are you ready?" (SYN-ACK)

Sir replies:
📱 "Yes Anuj, I'm at my desk. Are you also ready for the call?"
Sir acknowledges my request And asks for confirmation back.

TCP Equivalent: SYN-ACK packet - Server (Sir) says "Got your request, I'm available"


Step 3: Me : "Yes Sir, starting call now!" (ACK)

I immediately hits the video call button:
📱 [Video call initiated]
I confirm Sir's availability, and the actual conversation begins.

TCP Equivalent: ACK packet - Connection established, data flow starts

TCP 3-Way Handshake