<?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 Working: 3-Way Handshake]]></title><description><![CDATA[TCP Working: 3-Way Handshake]]></description><link>https://shubh-tcpworking.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Mon, 21 Sep 2026 19:16:50 GMT</lastBuildDate><atom:link href="https://shubh-tcpworking.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[Imagine trying to have a phone conversation where you can't hear the other person, you don't know if they received your message, and your words might arrive out of order or get corrupted. Frustrating, right? That's essentially what happens when data ...]]></description><link>https://shubh-tcpworking.hashnode.dev/tcp-working-3-way-handshake-and-reliable-communication</link><guid isPermaLink="true">https://shubh-tcpworking.hashnode.dev/tcp-working-3-way-handshake-and-reliable-communication</guid><category><![CDATA[TCP]]></category><category><![CDATA[UDP]]></category><category><![CDATA[3wayhandshake]]></category><dc:creator><![CDATA[Shubham Mourya]]></dc:creator><pubDate>Sun, 01 Feb 2026 01:51:36 GMT</pubDate><content:encoded><![CDATA[<p>Imagine trying to have a phone conversation where you can't hear the other person, you don't know if they received your message, and your words might arrive out of order or get corrupted. Frustrating, right? That's essentially what happens when data is sent across a network without rules or guarantees.</p>
<p>On the internet, data travels through unreliable networks filled with routers, switches, and countless devices. Packets can be lost, duplicated, damaged, or arrive out of sequence. Without a protocol to manage this chaos, applications would receive garbled, incomplete, or jumbled data. A file download could corrupt mid-transfer. A banking transaction might complete twice. A video call would be incomprehensible.</p>
<p>This is where TCP (Transmission Control Protocol) enters the picture as a lifesaver for reliable communication.</p>
<p><strong>What is TCP</strong></p>
<p>TCP is a connection-oriented protocol that sits at the Transport Layer (Layer 4) of the Internet model. Unlike UDP, which simply sends data without care about delivery, TCP ensures that:</p>
<ul>
<li><p>All data arrives at the destination, even if packets are lost during transmission</p>
</li>
<li><p>Data arrives in order, exactly as the sender send the request</p>
</li>
<li><p>Data is uncorrupted, verified using checksums</p>
</li>
<li><p>Both endpoints are ready before communication starts</p>
</li>
<li><p>Communication is gracefully closed when complete</p>
</li>
</ul>
<p><strong>Problems TCP is Designed to Solve</strong></p>
<ol>
<li><p>Packet Loss</p>
<p> Networks are unreliable. Routers can be congested, links can fail, and packets get dropped. TCP detects when packets are missing and automatically retransmits them.</p>
</li>
<li><p>Out-of-Order Delivery</p>
<p> In complex networks, packets taking different routes might arrive in the wrong sequence. TCP reassembles them using sequence numbers, ensuring applications receive data in the correct order</p>
</li>
<li><p>No Flow Control</p>
<p> fast sender overwhelming a slower receiver's buffer causes data loss. TCP uses a sliding window mechanism to control transmission rates based on receiver capacity</p>
</li>
<li><p>No Connection State</p>
<p> Without a handshake, how do both sides agree to communicate? TCP's 3-way handshake establishes a reliable connection before any data is exchanged</p>
</li>
</ol>
<p><strong>The TCP 3-Way Handshake: Establishing Trust</strong></p>
<p>Before two computers exchange data over TCP, they must establish a connection through a process called the 3-way handshake. This elegant mechanism ensures both computers are ready and have agreed on initial sequence numbers.</p>
<p>The handshake is like two people confirming they can hear each other:</p>
<ul>
<li><p>Person A : "Can you hear me? I'm starting from word number 100”</p>
</li>
<li><p>Person B : “Yes, I heard you! You said word 100. I'm starting from word 500“</p>
</li>
<li><p>Person C : “Perfect! I heard you say word 500. Let's start talking“</p>
</li>
</ul>
<p>Step 1: Client Sends SYN</p>
<p>The initiating computer (client) sends a SYN (Synchronize) packet to the server. This packet contains:</p>
<ul>
<li><p>SYN flag set to 1 – indicating this is a synchronization request</p>
</li>
<li><p>Initial Sequence Number (ISN) – a random starting number for byte sequencing (e.g., 1000)</p>
</li>
<li><p>Window size – how much data the client can accept</p>
</li>
</ul>
<p>The client enters the SYN-SENT state, waiting for a response. Think of this as “Hello, I am ready to talk, and I am starting my count at 1000“</p>
<p>Step 2: Server Responds with SYN-ACK</p>
<p>The server, already in LISTEN state, receives the SYN packet. It responds immediately with a SYN-ACK packet containing:</p>
<ul>
<li><p>ACK flag set to 1 – acknowledging the client's SYN</p>
</li>
<li><p>Acknowledgement Number = ISN_Client + 1 – confirming receipt of the client's starting number (e.g., 1001)</p>
</li>
<li><p>SYN flag set to 1 – the server's own synchronization request</p>
</li>
<li><p>Server's Initial Sequence Number – the server's starting number (e.g., 2000)</p>
</li>
<li><p>Window size – the server's receive capacity</p>
</li>
</ul>
<p>The server enters tge SYN-RECEIVED state. This says:” I got your message starting at 1000. I'm starting my count at 2000”</p>
<p>Step 3: Client Acknowledges with ACK</p>
<p>The client receives the SYN-ACK and responds with an ACK packet:</p>
<ul>
<li><p>ACK flag set to 1</p>
</li>
<li><p>Acknowledgement Number = ISN_Server + 1 – confirming the server's sequence number (e.g., 2001)</p>
</li>
</ul>
<p>Both client and server now enter the ESTABLISHED state, and the connection is ready for data transfer. This acknowledgement says: "I got your message starting at 2000. We're connected</p>
<p>How Data Transfer works in TCP</p>
<p>Once the connection is established, data flows bidirectionally using sequence numbers and acknowledgements.</p>
<p>Sequence Numbers : Tracking Every Byte</p>
<p>Every byte transmitted over TCP has a sequence number. When the client sends data, it includes-</p>
<ul>
<li><p>Sequence Number – the number of the first byte in this segment (e.g., 1000)</p>
</li>
<li><p>Data – actual payload (e.g., 500 bytes)</p>
</li>
<li><p>Length – how many bytes are in this segment</p>
</li>
</ul>
<p>If a segment contains bytes 1000-1499, the sequence number is 1000. The next segment would start at 1500.</p>
<p>Acknowledgement Numbers:Confirming Receipt</p>
<p>When the receiver gets data, it sends back an ACK packet with:</p>
<ul>
<li>Acknowledgement Number – the sequence number of the NEXT byte it expects (e.g., 1500)</li>
</ul>
<p>The number confirms: "I've successfully received all bytes up to 1499, and I'm ready for byte 1500 next”</p>
<p>How TCP Ensures Reliability: The Safety Net</p>
<p>TCP guarantees reliability through four complementary mechanisms:</p>
<ol>
<li><p>Positive Acknowledgement with Retransmission (PAR)</p>
<p> Every segment sent must be acknowledged. If no acknowledgement arrives within a timeout period, the sender assumes the packet was lost and retransmits it.</p>
</li>
<li><p>Sequence Numbers and Ordering</p>
<p> Each byte is numbered sequentially. Out-of-order packets are reassembled correctly using these numbers, and duplicates are discarded.</p>
</li>
<li><p>Checksums for Error Detection</p>
<p> TCP includes a checksum (calculated from the segment's contents) in every packet. If the checksum doesn't match upon arrival, the segment is discarded and will be retransmitted.</p>
</li>
<li><p>Retransmission Timeout (RTO) and Exponential Backoff</p>
<p> If an acknowledgement isn't received within the RTO window, the sender retransmits the packet. If it fails again, the RTO is doubled (exponential backoff: RTO → 2×RTO → 4×RTO → 8×RTO) to avoid overwhelming a congested network.</p>
</li>
</ol>
<p>Detecting Packet Loss: Two Methods</p>
<p>Method 1: Timeout Expiry</p>
<p>The sender sets a timer when transmitting a segment. If the timer expires without an acknowledgement, the segment is assumed lost and retransmitted.</p>
<p>Method 2: Duplicate Acknowledgements</p>
<p>If the receiver gets packets out of order, it sends duplicate ACKs for the same sequence number. If the sender receives multiple duplicate ACKs, it assumes a packet was lost and can retransmit immediately without waiting for the timeout.</p>
<p><strong>How TCP Connection Closes: Graceful Termination</strong></p>
<p>Closing a TCP connection properly ensures no data is left hanging. Unlike the 3-way handshake, closing involves a 4-way handshake because each side must signal independently that it's done sending data</p>
<p>The 4-way Closing Handsake</p>
<p>Step 1: Client Initiates Close (Sends FIN)</p>
<p>The client sends a FIN (Finish) flag packet, indicating "I have no more data to send." The client enters FIN-WAIT-1 state.</p>
<p>Step 2: Server Acknowledges (Sends ACK)</p>
<p>The server receives the FIN and acknowledges it with an ACK packet. The server enters CLOSE-WAIT state, but can still send data if needed.</p>
<p>Step 3: Server Closes (Sends FIN)</p>
<p>Once the server finishes sending any remaining data, it sends its own FIN packet. The server enters LAST-ACK state, waiting for the client's final acknowledgement.</p>
<p>Step 4: Client Acknowledges (Sends ACK)</p>
<p>The client receives the server's FIN and sends a final ACK. The client enters TIME-WAIT state for a brief period to handle any delayed packets, then fully closes.</p>
]]></content:encoded></item></channel></rss>