Oh My Algorithm
Concept Guide3-way · 4-way

TCP Handshake

Before exchanging data, TCP confirms both directions are ready with a 3-way handshake, and when finishing it closes each direction in turn with a 4-way handshake. Each step exchanges SYN, ACK, and FIN flags along with sequence numbers (seq/ack), and both sides' connection state changes step by step.

01TCP Handshake

3-Way Handshake · Connection Setup

A TCP connection confirms both sides are ready with three messages. Before it starts, the client is CLOSED and the server is LISTEN, waiting for a connection.

① The client sends SYN (connection request), passing its initial sequence number x, and enters SYN-SENT.

② The server replies with SYN+ACK, accepting the request (ACK) while sending its own sequence number y, and enters SYN-RCVD.

③ The client confirms the server's reply with ACK. At this moment both sides are ESTABLISHED — the connection is open.

3-way done · in just three exchanges both directions are ready to communicate. Real data can now flow.

💻Client
CLOSED
🗄️Server
LISTEN
SYN
SYN + ACK
ACK
1 / 5

4-Way Handshake · Connection Teardown

Closing must close each direction separately, so usually four messages go back and forth. Both sides start ESTABLISHED.

① The client sends FIN — a close request meaning "no more data to send" — and enters FIN-WAIT-1.

② The server confirms with ACK. Only the client→server direction closes; the server enters CLOSE-WAIT and can still finish sending its remaining data.

③ Once the server has sent all its data, it sends FIN and enters LAST-ACK, waiting for the final ACK.

④ The client sends the final ACK. The server fully closes to CLOSED, and the client waits briefly in TIME-WAIT.

4-way done · the client waits out TIME-WAIT (2MSL) to absorb stray packets, then becomes CLOSED and the connection fully ends.

💻Client
ESTABLISHED
🗄️Server
ESTABLISHED
FIN
ACK
FIN
ACK
1 / 6

02 Understand It Simply

For Everyone
🔑How It Works

Opening a connection exchanges SYN, SYN+ACK and ACK so both sides confirm they can send and receive. Closing shuts each direction separately, which takes four messages: FIN and ACK each way.

💡In Plain Words

Opening a connection exchanges SYN → SYN+ACK → ACK three times to confirm both sides are ready to send and receive (ESTABLISHED).

Closing must close one direction at a time, so FIN → ACK → FIN → ACK goes back and forth four times, and at the end it waits briefly in TIME-WAIT for stray packets before fully closing.

📍Where It's Used
  • Understanding how reliable connections (HTTP
  • HTTPS
  • SSH) work
  • diagnosing connection delays and port states (TIME-WAIT)
  • analyzing firewall and load-balancer behavior

03 Frequently Asked Questions

FAQ
What is TCP Handshake?+

Before exchanging data, TCP confirms both directions are ready with a 3-way handshake, and when finishing it closes each direction in turn with a 4-way handshake. Each step exchanges SYN, ACK, and FIN flags along with sequence numbers (seq/ack), and both sides' connection state changes step by step.

Where is TCP Handshake used?+

Understanding how reliable connections (HTTP, HTTPS, SSH) work, diagnosing connection delays and port states (TIME-WAIT), analyzing firewall and load-balancer behavior.

What's a simple analogy for TCP Handshake?+

Opening a connection exchanges SYN, SYN+ACK and ACK so both sides confirm they can send and receive. Closing shuts each direction separately, which takes four messages: FIN and ACK each way.