<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Mastering the TCP 3-Way Handshake]]></title><description><![CDATA[Mastering the TCP 3-Way Handshake]]></description><link>https://mastering-the-tcp-3-way-handshake.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 31 Oct 2026 07:37:35 GMT</lastBuildDate><atom:link href="https://mastering-the-tcp-3-way-handshake.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[TCP Working: 3-Way Handshake & Reliable]]></title><description><![CDATA[In the chaotic world of the internet, millions of "packets" of data are flying around every second. Without a strict set of rules, these packets would get…]]></description><link>https://mastering-the-tcp-3-way-handshake.hashnode.dev/tcp-working-3-way-handshake-and-reliable</link><guid isPermaLink="true">https://mastering-the-tcp-3-way-handshake.hashnode.dev/tcp-working-3-way-handshake-and-reliable</guid><category><![CDATA[TCP]]></category><category><![CDATA[three way handshake]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[Chaiaurcode]]></category><dc:creator><![CDATA[sourav halder]]></dc:creator><pubDate>Sun, 15 Feb 2026 17:53:24 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/dyUp7WPu5q4/upload/a7127320e510086dda9acb791dc1f46c.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In the chaotic world of the internet, millions of "packets" of data are flying around every second. Without a strict set of rules, these packets would get lost, arrive out of order, or show up corrupted.</p>
<p>Imagine trying to send a book to a friend by mailing each page in a separate envelope. Without a system, your friend might receive page 10 before page 1, or page 5 might never show up at all. <strong>TCP (Transmission Control Protocol)</strong> is the system that ensures the book arrives perfectly, page by page.</p>
<hr />
<h2 id="heading-the-problem-what-happens-without-rules">The Problem: What Happens Without Rules?</h2>
<p>Imagine you're trying to have a conversation across a crowded, noisy room. You shout "Hello!" but the other person doesn't hear you. You yell louder. They catch part of it but miss some words. You repeat yourself. Messages arrive out of order. Chaos.</p>
<p>This is what happens when computers try to communicate over the internet without a protocol.</p>
<p><strong>The challenges:</strong></p>
<ul>
<li><p><strong>Packets get lost</strong> - Data travels through multiple routers and networks. Sometimes packets disappear.</p>
</li>
<li><p><strong>Packets arrive out of order</strong> - The internet routes packets independently. Packet #3 might arrive before packet #1.</p>
</li>
<li><p><strong>Packets get corrupted</strong> - Electrical interference or hardware errors can flip bits during transmission.</p>
</li>
<li><p><strong>No confirmation</strong> - If you send data, how do you know it was received?</p>
</li>
<li><p><strong>No flow control</strong> - A fast sender can overwhelm a slow receiver.</p>
</li>
</ul>
<p><strong>Without addressing these problems, the internet wouldn't work.</strong> You'd download corrupted files, web pages would load with missing pieces, and video calls would be unintelligible.</p>
<p>This is where TCP comes in.</p>
<hr />
<h2 id="heading-what-is-tcp-and-why-do-we-need-it">What is TCP and Why Do We Need It?</h2>
<p>TCP is a "connection-oriented" protocol. Unlike its faster, messier cousin UDP (which just throws data at the receiver), TCP insists on a formal introduction before any data is sent.</p>
<p>It was designed to solve three major problems:</p>
<ol>
<li><p><strong>Packet Loss:</strong> Data getting lost in transit.</p>
</li>
<li><p><strong>Out-of-Order Delivery:</strong> Data arriving in the wrong sequence.</p>
</li>
<li><p><strong>Data Corruption:</strong> Data getting mangled during the journey.</p>
</li>
</ol>
<hr />
<p><strong>TCP vs UDP:</strong></p>
<p>TCP isn't the only transport protocol. There's also <strong>UDP (User Datagram Protocol)</strong>, which is faster but unreliable. UDP just sends data and hopes for the best—no guarantees, no handshakes, no retransmissions.</p>
<ul>
<li><p><strong>TCP:</strong> Reliable, ordered, slow. Used for web browsing, email, file transfers.</p>
</li>
<li><p><strong>UDP:</strong> Fast, unreliable, no guarantees. Used for video streaming, gaming, DNS lookups.</p>
</li>
</ul>
<p>Choose TCP when you need reliability. Choose UDP when you need speed and can tolerate some loss</p>
<hr />
<h2 id="heading-the-tcp-3-way-handshake-establishing-a-connection">The TCP 3-Way Handshake: Establishing a Connection</h2>
<p>Before any data is sent, TCP establishes a connection using a <strong>3-way handshake</strong>. This ensures both client and server are ready to communicate.</p>
<p><strong>Think of it like a phone call:</strong></p>
<ul>
<li><p><strong>You:</strong> "Hello? Can you hear me?"</p>
</li>
<li><p><strong>Friend:</strong> "Yes, I can hear you. Can you hear me?"</p>
</li>
<li><p><strong>You:</strong> "Yes! Okay, let's talk."</p>
</li>
</ul>
<p>Only after this exchange do you start your actual conversation. TCP does the same thing.</p>
<h3 id="heading-the-three-steps">The Three Steps</h3>
<p>The handshake involves three messages:</p>
<ol>
<li><p><strong>SYN</strong> (Synchronize) - Client to Server</p>
</li>
<li><p><strong>SYN-ACK</strong> (Synchronize-Acknowledge) - Server to Client</p>
</li>
<li><p><strong>ACK</strong> (Acknowledge) - Client to Server</p>
</li>
</ol>
<p>After these three messages, the connection is established and data transfer can begin.</p>
<h3 id="heading-step-1-client-sends-syn">Step 1: Client Sends SYN</h3>
<p><strong>What happens:</strong></p>
<p>The client wants to establish a connection. It sends a packet with:</p>
<ul>
<li><p><strong>SYN flag</strong> set to 1 (this marks it as a synchronization request)</p>
</li>
<li><p><strong>Sequence number</strong> (let's say 1000) - a random starting number</p>
</li>
</ul>
<p><strong>Meaning:</strong> "Hi, I want to connect. My starting sequence number is 1000. Are you ready?"</p>
<p><strong>Technical details:</strong></p>
<pre><code class="lang-bash">Client → Server
SYN: 1
Sequence Number: 1000
</code></pre>
<p>The client is now in <strong>SYN_SENT</strong> state, waiting for a response.</p>
<h3 id="heading-step-2-server-sends-syn-ack">Step 2: Server Sends SYN-ACK</h3>
<p><strong>What happens:</strong></p>
<p>If the server is willing to accept the connection, it responds with:</p>
<ul>
<li><p><strong>SYN flag</strong> set to 1 (server also wants to synchronize)</p>
</li>
<li><p><strong>ACK flag</strong> set to 1 (acknowledging the client's SYN)</p>
</li>
<li><p><strong>Sequence number</strong> (let's say 5000) - the server's random starting number</p>
</li>
<li><p><strong>Acknowledgment number</strong> set to 1001 (client's sequence + 1)</p>
</li>
</ul>
<p><strong>Meaning:</strong> "I received your request. My starting sequence number is 5000. I'm ready. I'm expecting your next byte to be 1001."</p>
<p><strong>Technical details:</strong></p>
<pre><code class="lang-bash">Server → Client
SYN: 1
ACK: 1
Sequence Number: 5000
Acknowledgment Number: 1001
</code></pre>
<p>The server is now in <strong>SYN_RECEIVED</strong> state.</p>
<h3 id="heading-step-3-client-sends-ack">Step 3: Client Sends ACK</h3>
<p><strong>What happens:</strong></p>
<p>The client acknowledges the server's response:</p>
<ul>
<li><p><strong>ACK flag</strong> set to 1</p>
</li>
<li><p><strong>Sequence number</strong> 1001 (as the server expected)</p>
</li>
<li><p><strong>Acknowledgment number</strong> set to 5001 (server's sequence + 1)</p>
</li>
</ul>
<p><strong>Meaning:</strong> "Got it! I received your confirmation. Let's communicate."</p>
<p><strong>Technical details:</strong></p>
<pre><code class="lang-bash">Client → Server
ACK: 1
Sequence Number: 1001
Acknowledgment Number: 5001
</code></pre>
<p>The connection is now <strong>ESTABLISHED</strong> on both sides. Data transfer can begin.</p>
<h2 id="heading-visual-3-way-handshake">Visual: 3-Way Handshake</h2>
<pre><code class="lang-bash">CLIENT                                   SERVER
  │                                        │
  │  1. SYN (seq=1000)                     │
  │ ─────────────────────────────────────&gt; │
  │                                        │
  │  <span class="hljs-string">"I want to connect. My seq is 1000"</span>   │
  │                                        │
  │                                        │
  │  2. SYN-ACK (seq=5000, ack=1001)       │
  │ &lt;───────────────────────────────────── │
  │                                        │
  │  <span class="hljs-string">"Okay! My seq is 5000. I got yours"</span>   │
  │                                        │
  │                                        │
  │  3. ACK (seq=1001, ack=5001)           │
  │ ─────────────────────────────────────&gt; │
  │                                        │
  │  <span class="hljs-string">"Perfect! Connection established"</span>     │
  │                                        │
  │━━━━━━━━ CONNECTION ESTABLISHED ━━━━━━━│
  │                                        │
  │  Now data can flow both ways           │
  │                                        │
</code></pre>
<h2 id="heading-why-three-steps-why-not-two">Why Three Steps? Why Not Two?</h2>
<p>You might wonder: why does the client need to send a final ACK? Couldn't we establish the connection after just SYN and SYN-ACK?</p>
<p><strong>The reason:</strong> TCP is a two-way connection. Both sides need to:</p>
<ol>
<li><p>Announce their starting sequence numbers</p>
</li>
<li><p>Acknowledge the other side's sequence number</p>
</li>
</ol>
<p><strong>Two messages wouldn't be enough:</strong></p>
<ul>
<li><p>After SYN, the client knows the server is listening</p>
</li>
<li><p>After SYN-ACK, the server knows the client is listening</p>
</li>
<li><p>But the client doesn't yet know the server received confirmation</p>
</li>
</ul>
<p>The third ACK ensures both sides are synchronized and ready.</p>
<h2 id="heading-how-data-transfer-works-in-tcp">How Data Transfer Works in TCP</h2>
<p>Once the connection is established, data flows with sequence and acknowledgment numbers tracking everything.</p>
<h3 id="heading-sequence-numbers">Sequence Numbers</h3>
<p>Every byte of data has a sequence number. If the client sends 100 bytes starting at sequence 1001, those bytes are numbered 1001 through 1100.</p>
<p><strong>Example:</strong></p>
<p>Client sends 100 bytes of data:</p>
<pre><code class="lang-bash">Client → Server
Sequence Number: 1001
Data: [100 bytes]
</code></pre>
<p>The server receives it and acknowledges:</p>
<pre><code class="lang-bash">Server → Client
Acknowledgment Number: 1101
</code></pre>
<p>This ACK says: "I received bytes 1001-1100. I'm expecting byte 1101 next."</p>
<h3 id="heading-acknowledgments">Acknowledgments</h3>
<p>TCP uses <strong>cumulative acknowledgments</strong>. An ACK number means: "I've successfully received all bytes up to (but not including) this number."</p>
<p><strong>Example conversation:</strong></p>
<pre><code class="lang-bash">Client sends bytes 1001-1100
Server ACKs: 1101 (<span class="hljs-string">"Got everything up to 1100"</span>)

Client sends bytes 1101-1200
Server ACKs: 1201 (<span class="hljs-string">"Got everything up to 1200"</span>)

Client sends bytes 1201-1300
Server ACKs: 1301 (<span class="hljs-string">"Got everything up to 1300"</span>)
</code></pre>
<p>This continues until all data is transferred.</p>
<h3 id="heading-bidirectional-communication">Bidirectional Communication</h3>
<p>TCP is <strong>full-duplex</strong>, meaning both sides can send and receive simultaneously. Each direction has its own sequence numbers.</p>
<pre><code class="lang-bash">CLIENT                          SERVER
  │                               │
  │  Data (seq=1001-1100)         │
  │ ───────────────────────────&gt;  │
  │                               │
  │  Data (seq=5001-5200)         │
  │ &lt;─────────────────────────── │
  │                               │
  │  ACK (ack=5201)               │
  │ ───────────────────────────&gt;  │
  │                               │
  │  ACK (ack=1101)               │
  │ &lt;─────────────────────────── │
</code></pre>
<p>Both sides maintain their own sequence numbers and acknowledge the other side's data</p>
<hr />
<h2 id="heading-how-a-tcp-connection-is-closed">How a TCP Connection is Closed</h2>
<p>Just like establishing a connection requires a handshake, closing it also requires a proper procedure. TCP uses a <strong>4-way handshake</strong> for termination.</p>
<h3 id="heading-the-four-steps">The Four Steps</h3>
<ol>
<li><p><strong>FIN</strong> - Client to Server (or vice versa)</p>
</li>
<li><p><strong>ACK</strong> - Server to Client</p>
</li>
<li><p><strong>FIN</strong> - Server to Client</p>
</li>
<li><p><strong>ACK</strong> - Client to Server</p>
</li>
</ol>
<h3 id="heading-step-by-step-termination">Step-by-Step Termination</h3>
<p><strong>Step 1: Client Sends FIN</strong></p>
<p>The client is done sending data:</p>
<pre><code class="lang-bash">Client → Server
FIN: 1
Sequence Number: 1500
</code></pre>
<p>"I'm finished sending data."</p>
<p><strong>Step 2: Server Sends ACK</strong></p>
<p>The server acknowledges:</p>
<pre><code class="lang-bash">Server → Client
ACK: 1
Acknowledgment Number: 1501
</code></pre>
<p>"I got your FIN. I'm still processing."</p>
<p>At this point, the connection is <strong>half-closed</strong>. The client won't send more data, but the server can still send data to the client.</p>
<p><strong>Step 3: Server Sends FIN</strong></p>
<p>When the server is done:</p>
<pre><code class="lang-bash">Server → Client
FIN: 1
Sequence Number: 6000
</code></pre>
<p>"I'm finished sending data too."</p>
<p><strong>Step 4: Client Sends ACK</strong></p>
<p>The client acknowledges:</p>
<pre><code class="lang-bash">Client → Server
ACK: 1
Acknowledgment Number: 6001
</code></pre>
<p>"Got it. Connection closed."</p>
<p>The connection is now fully terminated.</p>
<h3 id="heading-why-four-steps">Why Four Steps?</h3>
<p>TCP is full-duplex—data flows in both directions independently. Each direction must be closed separately:</p>
<ul>
<li><p>Client → Server direction closes (FIN + ACK)</p>
</li>
<li><p>Server → Client direction closes (FIN + ACK)</p>
</li>
</ul>
<p>Total: 4 messages.</p>
<h2 id="heading-complete-example-loading-a-web-page">Complete Example: Loading a Web Page</h2>
<p>Let's trace a real-world scenario: you visit <a target="_blank" href="https://example.com"><code>https://example.com</code></a></p>
<p><strong>Step 1: DNS Lookup</strong> (not TCP, but necessary) Your browser resolves <a target="_blank" href="http://example.com"><code>example.com</code></a> to IP address <code>93.184.216.34</code></p>
<p><strong>Step 2: TCP 3-Way Handshake</strong></p>
<pre><code class="lang-bash">Browser → Server (93.184.216.34:443)
  SYN (seq=1000)

Server → Browser
  SYN-ACK (seq=5000, ack=1001)

Browser → Server
  ACK (seq=1001, ack=5001)
</code></pre>
<p>Connection established.</p>
<p><strong>Step 3: TLS Handshake</strong> (happens over TCP) HTTPS requires encryption, so a TLS handshake occurs here. We'll skip details.</p>
<p><strong>Step 4: HTTP Request</strong></p>
<pre><code class="lang-bash">Browser → Server
  Sequence: 1001
  Data: <span class="hljs-string">"GET / HTTP/1.1\r\nHost: example.com\r\n..."</span>

Server → Browser
  ACK (ack=1100)
</code></pre>
<p><strong>Step 5: HTTP Response</strong></p>
<pre><code class="lang-bash">Server → Browser
  Sequence: 5001
  Data: <span class="hljs-string">"HTTP/1.1 200 OK\r\n...&lt;html&gt;..."</span>

Browser → Server
  ACK (ack=7000)
</code></pre>
<p>Server may send multiple packets if the page is large.</p>
<p><strong>Step 6: Connection Termination</strong></p>
<pre><code class="lang-bash">Browser → Server
  FIN (seq=1100)

Server → Browser
  ACK (ack=1101)

Server → Browser
  FIN (seq=7000)

Browser → Server
  ACK (ack=7001)
</code></pre>
<p>Connection closed. Web page loaded.</p>
<p><strong>All of this happens in milliseconds.</strong></p>
<h2 id="heading-key-takeaways">Key Takeaways</h2>
<ul>
<li><p><strong>TCP is a reliable, ordered, connection-oriented protocol</strong> that solves the chaos of unreliable networks</p>
</li>
<li><p><strong>The 3-way handshake (SYN, SYN-ACK, ACK)</strong> establishes a connection and synchronizes both sides</p>
</li>
<li><p><strong>Sequence numbers and acknowledgments</strong> ensure data arrives in order and completely</p>
</li>
<li><p><strong>Retransmissions and timeouts</strong> handle packet loss</p>
</li>
<li><p><strong>Checksums</strong> detect corrupted data</p>
</li>
<li><p><strong>Flow control</strong> prevents fast senders from overwhelming slow receivers</p>
</li>
<li><p><strong>Congestion control</strong> prevents network overload</p>
</li>
<li><p><strong>The 4-way handshake (FIN, ACK, FIN, ACK)</strong> properly terminates connections</p>
</li>
<li><p><strong>TCP trades speed for reliability</strong> - it's slower than UDP but guarantees delivery</p>
</li>
</ul>
<hr />
<h2 id="heading-summary-why-tcp-is-the-backbone-of-the-web">Summary: Why TCP is the Backbone of the Web</h2>
<p>TCP is the unsung hero of the internet. While it adds a bit of "overhead" (extra time for handshakes and ACKs), it provides the <strong>guaranteed delivery</strong> that makes the modern web possible. Without it, every file download would be a gamble and every website would be a broken mess.</p>
]]></content:encoded></item></channel></rss>