<?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[TCP: 3-Way Handshake & Reliability]]></title><description><![CDATA[TCP: 3-Way Handshake & Reliability]]></description><link>https://pradhyumna8.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 22 Sep 2026 13:31:27 GMT</lastBuildDate><atom:link href="https://pradhyumna8.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 Communication]]></title><description><![CDATA[You know that feeling when you're watching a YouTube video and it just... works? Or when you're downloading a file and it arrives perfectly intact, not corrupted or missing chunks? Yeah, that's not magic. That's TCP doing its job so well that you nev...]]></description><link>https://pradhyumna8.hashnode.dev/tcp-working-3-way-handshake-and-reliable-communication</link><guid isPermaLink="true">https://pradhyumna8.hashnode.dev/tcp-working-3-way-handshake-and-reliable-communication</guid><category><![CDATA[internet]]></category><category><![CDATA[ChaiCode]]></category><category><![CDATA[coding]]></category><dc:creator><![CDATA[Pradhyumna]]></dc:creator><pubDate>Fri, 30 Jan 2026 16:54:52 GMT</pubDate><content:encoded><![CDATA[<hr />
<p>You know that feeling when you're watching a YouTube video and it just... works? Or when you're downloading a file and it arrives perfectly intact, not corrupted or missing chunks? Yeah, that's not magic. That's TCP doing its job so well that you never even think about it.</p>
<p>Let me take you on a journey into one of the most fundamental protocols that literally holds the internet together. And trust me, by the end of this, you'll have a newfound appreciation for the elegant chaos happening behind every click.</p>
<h2 id="heading-the-wild-west-what-happens-without-rules">The Wild West: What Happens Without Rules?</h2>
<p>Imagine you're trying to tell your friend a story, but you're both standing on opposite sides of a busy highway. You're shouting sentences across the traffic, and your friend is trying to piece together what you're saying.</p>
<p>Some problems that would immediately pop up:</p>
<ul>
<li><p><strong>Did they even hear you?</strong> Maybe a loud truck passed by during your first sentence.</p>
</li>
<li><p><strong>Did they hear you correctly?</strong> "I bought a cat" might sound like "I fought a bat" with all that noise.</p>
</li>
<li><p><strong>Did they get the order right?</strong> If sentence 3 arrives before sentence 2, your story makes no sense.</p>
</li>
<li><p><strong>Did they get everything?</strong> Maybe sentence 5 never made it across at all.</p>
</li>
</ul>
<p>This is essentially what happens when computers try to talk to each other over a network without any rules. The internet is like that busy highway—chaotic, unpredictable, and full of obstacles. Packets of data get lost, corrupted, delayed, or arrive out of order all the time.</p>
<p>So how do we fix this mess? Enter TCP.</p>
<h2 id="heading-what-is-tcp-and-why-should-you-care">What is TCP and Why Should You Care?</h2>
<p>TCP stands for <strong>Transmission Control Protocol</strong>, and it's basically the responsible adult in the room when computers need to communicate reliably.</p>
<p>Think of the internet as having different layers (kind of like a cake, but nerdier). At the bottom, you have the physical cables and Wi-Fi signals. At the top, you have your applications like Chrome or Spotify. TCP lives in the middle, in what we call the <strong>Transport Layer</strong>.</p>
<p>Its job? Simple in concept, complex in execution:</p>
<blockquote>
<p><strong>Make sure data gets from point A to point B reliably, in the correct order, and without errors.</strong></p>
</blockquote>
<p>TCP is what makes sure that when you load a webpage, you get the whole page—not just 80% of it with random chunks missing. It's why your downloaded PDF isn't corrupted. It's why video calls (mostly) work.</p>
<h2 id="heading-the-problems-tcp-was-born-to-solve">The Problems TCP Was Born to Solve</h2>
<p>Let's get specific about what TCP tackles:</p>
<h3 id="heading-1-packet-loss">1. <strong>Packet Loss</strong></h3>
<p>The internet doesn't have a dedicated highway for your data. Packets hop through multiple routers, switches, and networks. Sometimes they just... vanish. Maybe a router got overwhelmed. Maybe there was a temporary network glitch. TCP notices when packets go missing and resends them.</p>
<h3 id="heading-2-packets-arriving-out-of-order">2. <strong>Packets Arriving Out of Order</strong></h3>
<p>Your data gets split into packets, and each packet might take a different route to reach the destination. Packet #5 might arrive before packet #2. TCP keeps track of the order and reassembles everything correctly.</p>
<h3 id="heading-3-corrupted-data">3. <strong>Corrupted Data</strong></h3>
<p>Electrical interference, faulty hardware, cosmic rays (yes, really)—lots of things can flip bits in your data during transmission. TCP checks for corruption and discards bad packets, requesting fresh copies.</p>
<h3 id="heading-4-flow-control">4. <strong>Flow Control</strong></h3>
<p>What if the sender is blasting data faster than the receiver can process it? It's like trying to drink from a fire hose. TCP has mechanisms to slow things down when needed.</p>
<h3 id="heading-5-congestion-control">5. <strong>Congestion Control</strong></h3>
<p>When networks get crowded, dumping more data into them makes things worse. TCP can detect congestion and back off, being a good citizen of the internet.</p>
<h2 id="heading-the-tcp-3-way-handshake-lets-shake-hands-before-we-talk">The TCP 3-Way Handshake: Let's Shake Hands Before We Talk</h2>
<p>Before TCP sends any actual data, it establishes a connection using what's called the <strong>3-way handshake</strong>. This is like two people making sure they can hear each other before starting an important conversation.</p>
<p>Here's an analogy that helped me understand it:</p>
<p><strong>Imagine you're calling a friend:</strong></p>
<ol>
<li><p><strong>You:</strong> "Hey, can you hear me?" (SYN)</p>
</li>
<li><p><strong>Friend:</strong> "Yeah, I can hear you! Can you hear me?" (SYN-ACK)</p>
</li>
<li><p><strong>You:</strong> "Yep, I hear you too! Let's talk." (ACK)</p>
</li>
</ol>
<p>Now you both know the connection works both ways, and you can start your actual conversation.</p>
<p>In TCP terms:</p>
<ul>
<li><p><strong>SYN</strong> = Synchronize (initiating the connection)</p>
</li>
<li><p><strong>ACK</strong> = Acknowledgement (confirming receipt)</p>
</li>
</ul>
<p>Let's break this down step by step.</p>
<h2 id="heading-step-by-step-the-3-way-handshake-in-detail">Step-by-Step: The 3-Way Handshake in Detail</h2>
<h3 id="heading-step-1-syn-client-to-server"><strong>Step 1: SYN (Client to Server)</strong></h3>
<p>The client (let's say your web browser) wants to connect to a server (let's say <a target="_blank" href="http://google.com">google.com</a>).</p>
<p>Your browser sends a TCP packet with the <strong>SYN flag</strong> set to 1. This packet essentially says:</p>
<ul>
<li><p>"Hey server, I want to talk to you!"</p>
</li>
<li><p>"My initial sequence number is X" (let's say 1000)</p>
</li>
<li><p>"Here are my TCP options and capabilities"</p>
</li>
</ul>
<p>The sequence number is crucial—it's like numbering the pages of a book. This number will help both sides keep track of what's been sent and received.</p>
<p><strong>State change:</strong> Client goes from CLOSED to SYN-SENT state.</p>
<h3 id="heading-step-2-syn-ack-server-to-client"><strong>Step 2: SYN-ACK (Server to Client)</strong></h3>
<p>If the server is listening and willing to accept the connection, it responds with a packet that has BOTH the <strong>SYN and ACK flags</strong> set.</p>
<p>The server says:</p>
<ul>
<li><p>"Hey client, I received your SYN!" (ACK)</p>
</li>
<li><p>"I also want to establish a connection!" (SYN)</p>
</li>
<li><p>"My initial sequence number is Y" (let's say 5000)</p>
</li>
<li><p>"I acknowledge your sequence number X+1" (1001)</p>
</li>
</ul>
<p>That acknowledgement number (X+1) is the server saying "I got your message, and I'm expecting your next byte to be numbered 1001."</p>
<p><strong>State change:</strong> Server goes from LISTEN to SYN-RECEIVED state.</p>
<h3 id="heading-step-3-ack-client-to-server"><strong>Step 3: ACK (Client to Server)</strong></h3>
<p>Finally, the client sends one more acknowledgement:</p>
<ul>
<li><p>"I received your SYN-ACK!"</p>
</li>
<li><p>"I acknowledge your sequence number Y+1" (5001)</p>
</li>
<li><p>"Let's do this!"</p>
</li>
</ul>
<p><strong>State changes:</strong></p>
<ul>
<li><p>Client goes to ESTABLISHED</p>
</li>
<li><p>Server goes to ESTABLISHED</p>
</li>
</ul>
<p>The connection is now fully established, and both sides can start sending data!</p>
<h2 id="heading-visualizing-the-handshake">Visualizing the Handshake</h2>
<pre><code class="lang-bash">Client                                Server
  |                                     |
  |          SYN (seq=1000)             |
  |------------------------------------&gt;|
  |                                     |
  |    SYN-ACK (seq=5000, ack=1001)    |
  |&lt;------------------------------------|
  |                                     |
  |          ACK (ack=5001)             |
  |------------------------------------&gt;|
  |                                     |
  |         CONNECTION ESTABLISHED      |
  |                                     |
</code></pre>
<h2 id="heading-why-three-steps-couldnt-we-do-it-in-two">Why Three Steps? Couldn't We Do It in Two?</h2>
<p>Great question! Here's why we need that third step:</p>
<p>Imagine this scenario with just two steps:</p>
<ol>
<li><p>Client sends SYN</p>
</li>
<li><p>Server sends SYN-ACK</p>
</li>
</ol>
<p>But what if that SYN from the client was actually a delayed duplicate from an old connection attempt? The server would accept it and allocate resources, but the client wouldn't actually want that connection. The server would be sitting there waiting forever.</p>
<p>With the third ACK, the client explicitly confirms: "Yes, THIS is the connection I want RIGHT NOW." It prevents these weird half-open zombie connections.</p>
<h2 id="heading-how-data-transfer-works-sequence-numbers-are-your-friend">How Data Transfer Works: Sequence Numbers Are Your Friend</h2>
<p>Okay, connection established. Now let's send some data.</p>
<p>Every byte that gets transmitted has a sequence number. If the client's initial sequence number was 1000, and it sends 500 bytes of data, those bytes are numbered 1001 through 1500.</p>
<p>When the server receives this data, it sends back an ACK with the number 1501, meaning "I got all bytes up to 1500, send me 1501 next!"</p>
<h3 id="heading-heres-a-simple-data-transfer-flow">Here's a Simple Data Transfer Flow:</h3>
<pre><code class="lang-bash">Client                                Server
  |                                     |
  |   Data (seq=1001, 500 bytes)       |
  |------------------------------------&gt;|
  |                                     |
  |         ACK (ack=1501)              |
  |&lt;------------------------------------|
  |                                     |
  |   Data (seq=1501, 300 bytes)       |
  |------------------------------------&gt;|
  |                                     |
  |         ACK (ack=1801)              |
  |&lt;------------------------------------|
</code></pre>
<p>The sequence numbers create a paper trail of what's been sent and confirmed.</p>
<h2 id="heading-how-tcp-ensures-reliability-the-safety-nets">How TCP Ensures Reliability: The Safety Nets</h2>
<p>This is where TCP really shines. It has multiple mechanisms to ensure reliability:</p>
<h3 id="heading-1-acknowledgements">1. <strong>Acknowledgements</strong></h3>
<p>Every segment sent requires an acknowledgement. If the sender doesn't receive an ACK within a certain time (the timeout period), it assumes the packet was lost and retransmits it.</p>
<p>Think of it like texting a friend. If they don't reply within a reasonable time, you might send "Did you get my last message?"</p>
<h3 id="heading-2-sequence-numbers-for-ordering">2. <strong>Sequence Numbers for Ordering</strong></h3>
<p>Even if packets arrive out of order (packet 5 before packet 3), the receiver can use sequence numbers to put them back in the right order before passing data to the application.</p>
<p>It's like receiving pages of a book randomly but being able to reorganize them because they're numbered.</p>
<h3 id="heading-3-checksums-for-correctness">3. <strong>Checksums for Correctness</strong></h3>
<p>Every TCP segment includes a checksum—a mathematical value calculated from the data. The receiver calculates its own checksum and compares. If they don't match, the data was corrupted in transit, and the packet is discarded (which triggers a retransmission when the ACK never arrives).</p>
<h3 id="heading-4-retransmission">4. <strong>Retransmission</strong></h3>
<p>When the sender doesn't receive an ACK within the timeout period, it retransmits the data. TCP keeps trying until it gets confirmation or gives up after too many attempts.</p>
<h3 id="heading-visualizing-packet-loss-and-retransmission">Visualizing Packet Loss and Retransmission:</h3>
<pre><code class="lang-bash">Client                                Server
  |                                     |
  |   Data (seq=1001, 500 bytes)       |
  |------------------------------------&gt;|
  |                                     |
  |         ACK (ack=1501)              |
  |&lt;------------------------------------|
  |                                     |
  |   Data (seq=1501, 300 bytes)       |
  |-------------- X (LOST!)             |
  |                                     |
  |   [Timeout expires]                 |
  |                                     |
  |   Data (seq=1501, 300 bytes)       |
  |   [RETRANSMISSION]                  |
  |------------------------------------&gt;|
  |                                     |
  |         ACK (ack=1801)              |
  |&lt;------------------------------------|
</code></pre>
<p>The beauty here? The application never knows this happened. TCP handled it silently in the background.</p>
<h2 id="heading-flow-control-dont-overwhelm-your-partner">Flow Control: Don't Overwhelm Your Partner</h2>
<p>TCP uses something called the <strong>receive window</strong> to implement flow control.</p>
<p>The receiver tells the sender: "I have X bytes of buffer space available." The sender won't send more data than this window allows.</p>
<p>It's like one person dictating notes while another writes them down. If the writer says "Slow down, I can't keep up!" the dictator pauses until the writer is ready for more.</p>
<p>This prevents the receiver from being flooded with data it can't process, which would lead to packet drops and inefficiency.</p>
<h2 id="heading-congestion-control-being-a-good-internet-citizen">Congestion Control: Being a Good Internet Citizen</h2>
<p>TCP also monitors network congestion and adjusts its sending rate accordingly.</p>
<p>Algorithms like <strong>Slow Start</strong>, <strong>Congestion Avoidance</strong>, <strong>Fast Retransmit</strong>, and <strong>Fast Recovery</strong> help TCP:</p>
<ul>
<li><p>Start slowly and ramp up speed</p>
</li>
<li><p>Detect when the network is congested (packets getting lost)</p>
</li>
<li><p>Back off when needed</p>
</li>
<li><p>Recover quickly when congestion clears</p>
</li>
</ul>
<p>This is why when you start downloading a large file, the speed might start low and gradually increase as TCP figures out how much bandwidth is available.</p>
<h2 id="heading-closing-a-tcp-connection-the-polite-goodbye">Closing a TCP Connection: The Polite Goodbye</h2>
<p>When communication is done, TCP doesn't just drop the connection. It closes it gracefully using a 4-step process (sometimes called the <strong>4-way handshake</strong>).</p>
<h3 id="heading-the-closing-process">The Closing Process:</h3>
<p>Let's say the client wants to close the connection.</p>
<p><strong>Step 1: FIN from Client</strong></p>
<ul>
<li><p>Client sends a packet with the <strong>FIN flag</strong> (finish) set</p>
</li>
<li><p>"I'm done sending data"</p>
</li>
<li><p>State: FIN-WAIT-1</p>
</li>
</ul>
<p><strong>Step 2: ACK from Server</strong></p>
<ul>
<li><p>Server acknowledges the FIN</p>
</li>
<li><p>"Got it, you're done sending"</p>
</li>
<li><p>Server can still send data if it needs to</p>
</li>
<li><p>State: CLOSE-WAIT on server, FIN-WAIT-2 on client</p>
</li>
</ul>
<p><strong>Step 3: FIN from Server</strong></p>
<ul>
<li><p>When server is also done sending data, it sends its own FIN</p>
</li>
<li><p>"Okay, I'm done too"</p>
</li>
</ul>
<p><strong>Step 4: ACK from Client</strong></p>
<ul>
<li><p>Client acknowledges the server's FIN</p>
</li>
<li><p>"Got it, closing connection"</p>
</li>
<li><p>State: TIME-WAIT, then CLOSED</p>
</li>
</ul>
<h3 id="heading-visualizing-connection-termination">Visualizing Connection Termination:</h3>
<pre><code class="lang-bash">Client                                Server
  |                                     |
  |            FIN (seq=X)              |
  |------------------------------------&gt;|
  |                                     |
  |           ACK (ack=X+1)             |
  |&lt;------------------------------------|
  |                                     |
  |            FIN (seq=Y)              |
  |&lt;------------------------------------|
  |                                     |
  |           ACK (ack=Y+1)             |
  |------------------------------------&gt;|
  |                                     |
  |         CONNECTION CLOSED           |
</code></pre>
<p>Why four steps instead of three? Because TCP is <strong>full-duplex</strong>—both sides can send data independently. When one side says "I'm done sending," the other might still have data to send. Each direction of the connection is closed independently.</p>
<h2 id="heading-the-complete-tcp-lifecycle">The Complete TCP Lifecycle</h2>
<p>Let's see the entire journey:</p>
<pre><code class="lang-bash">CLOSED
  ↓
[3-Way Handshake]
  ↓
ESTABLISHED
  ↓
[Data Transfer with ACKs]
  ↓
[4-Way Handshake]
  ↓
CLOSED
</code></pre>
<p>From the moment your browser connects to a server, to the data transfer, to the graceful closure—TCP manages it all.</p>
<h2 id="heading-real-world-example-loading-a-webpage">Real-World Example: Loading a Webpage</h2>
<p>Let's put it all together with something you do every day.</p>
<p>You type <a target="_blank" href="http://www.example.com"><code>www.example.com</code></a> in your browser and hit Enter. Here's what TCP does:</p>
<ol>
<li><p><strong>DNS lookup</strong> happens first (that's UDP, actually, but let's skip that)</p>
</li>
<li><p><strong>3-way handshake</strong> establishes TCP connection to the web server (port 80 or 443)</p>
</li>
<li><p><strong>HTTP request</strong> is sent over the TCP connection: "GET / HTTP/1.1"</p>
</li>
<li><p><strong>Server processes</strong> and starts sending the HTML back</p>
</li>
<li><p><strong>Data flows</strong> with sequence numbers and acknowledgements</p>
</li>
<li><p><strong>Retransmissions happen</strong> automatically if any packets are lost</p>
</li>
<li><p><strong>Connection closes</strong> after all data is transferred (or kept alive for future requests)</p>
</li>
</ol>
<p>All of this happens in milliseconds, and you just see the webpage load.</p>
<h2 id="heading-why-tcp-isnt-always-the-answer">Why TCP Isn't Always the Answer</h2>
<p>TCP is amazing, but it's not perfect for everything.</p>
<p><strong>When TCP is great:</strong></p>
<ul>
<li><p>Web browsing (HTTP/HTTPS)</p>
</li>
<li><p>Email (SMTP, IMAP)</p>
</li>
<li><p>File transfers (FTP, SFTP)</p>
</li>
<li><p>SSH connections</p>
</li>
<li><p>Anything where reliability matters more than speed</p>
</li>
</ul>
<p><strong>When TCP is NOT ideal:</strong></p>
<ul>
<li><p>Live video streaming (some packet loss is acceptable for speed)</p>
</li>
<li><p>Online gaming (low latency is critical, reliability less so)</p>
</li>
<li><p>Voice calls (VoIP)</p>
</li>
<li><p>DNS queries (quick one-off requests)</p>
</li>
</ul>
<p>For these use cases, <strong>UDP</strong> (User Datagram Protocol) is often preferred. UDP is like TCP's younger, faster, but less reliable sibling. No handshakes, no guaranteed delivery, no ordering—just fire and forget.</p>
<h2 id="heading-common-misconceptions-about-tcp">Common Misconceptions About TCP</h2>
<p><strong>"TCP is slow"</strong> Not really. TCP adds overhead, yes, but modern implementations are highly optimized. The reliability is worth the tiny bit of extra time.</p>
<p><strong>"TCP guarantees delivery"</strong> Kind of. TCP will try very hard to deliver your data, but if the network is completely down or the other side disappears, it will eventually give up and report an error.</p>
<p><strong>"Every byte is acknowledged individually"</strong> Nope. TCP uses cumulative acknowledgements. An ACK for byte 5000 means "I've received everything up to byte 5000."</p>
<h2 id="heading-wrapping-up-tcp-is-everywhere">Wrapping Up: TCP is Everywhere</h2>
<p>The next time you:</p>
<ul>
<li><p>Send an email</p>
</li>
<li><p>Load a webpage</p>
</li>
<li><p>Upload a photo</p>
</li>
<li><p>Download a file</p>
</li>
<li><p>Connect via SSH</p>
</li>
</ul>
<p>Remember that TCP is working tirelessly behind the scenes, making sure every single byte arrives correctly, in order, and intact.</p>
<p>It's one of those technologies that's so good at its job that it's invisible—until something goes wrong, and then you realize how much you depend on it.</p>
<p>The 3-way handshake, sequence numbers, acknowledgements, retransmissions, and graceful closures all work together to create the reliable internet experience we take for granted.</p>
<p>Pretty cool for a protocol designed in the 1970s, right?</p>
<hr />
<p><strong>Further Reading:</strong></p>
<ul>
<li><p>RFC 793: The original TCP specification</p>
</li>
<li><p>"TCP/IP Illustrated" by W. Richard Stevens</p>
</li>
<li><p>Wireshark: Capture and analyze actual TCP traffic on your network</p>
</li>
</ul>
<p><strong>Questions? Comments?</strong> Drop them below! I'd love to hear if this helped clarify TCP for you or if there's anything you'd like me to dive deeper into.</p>
<p><em>Happy networking! 🌐</em></p>
<hr />
]]></content:encoded></item></channel></rss>