> For the complete documentation index, see [llms.txt](https://mominur.gitbook.io/mominur-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mominur.gitbook.io/mominur-notes/random-topics/three-way-handshake.md).

# Three-way handshake

The **three-way handshake** is a process used in **TCP (Transmission Control Protocol)** to establish a reliable connection between a client and a server over a network. It ensures that both sides are ready to communicate and agree on initial sequence numbers for data transfer. It’s called a “three-way” handshake because it involves three steps. Here’s how it works:

***

#### Step 1: **SYN (Synchronize)**

* The **client** sends a TCP segment with the **SYN** flag set to the **server**.
* This indicates that the client wants to establish a connection.
* The segment also includes an **initial sequence number (ISN)** for the client.

***

#### Step 2: **SYN-ACK (Synchronize-Acknowledge)**

* The **server** responds with a TCP segment that has both **SYN** and **ACK** flags set.
* **SYN:** Server chooses its own sequence number for the connection.
* **ACK:** Acknowledges the client’s SYN by incrementing the client’s ISN by 1.

***

#### Step 3: **ACK (Acknowledge)**

* The **client** sends a final **ACK** to the server.
* This acknowledges the server’s SYN by incrementing the server’s ISN by 1.
* Once this is done, the connection is established, and data transfer can begin.

***

Imagine you’re meeting someone for the first time:

1. You wave to say, “Hello, I want to talk.” → **SYN**
2. They wave back, “Hi, I’m ready too.” → **SYN-ACK**
3. You nod, “Great, let’s start.” → **ACK**

Now both sides know they’re ready to communicate.
