Skip to main content

Command Palette

Search for a command to run...

TCP Working: 3-Way Handshake & Reliable Communication

Published
3 min read

The Problem: Unreliable Data Transfer

Without rules, data sent over networks would be chaotic:

  • Packets could arrive out of order

  • Data could be corrupted

  • Packets might get lost

  • No way to know if the recipient is ready

TCP solves these problems by providing reliable, ordered, and error-checked data delivery.

TCP 3-Way Handshake: Establishing a Connection

Before data transfer, TCP establishes a connection using a three-step process:

Step-by-Step Breakdown:

  1. SYN (Synchronize)

    • Client sends: "I want to connect. My starting sequence number is 100."

    • Contains: SYN flag set, Initial Sequence Number (ISN=100)

  2. SYN-ACK (Synchronize-Acknowledge)

    • Server responds: "I acknowledge your request (101). I accept. My starting number is 300."

    • Contains: SYN and ACK flags set, Acknowledgment Number=101, ISN=300

  3. ACK (Acknowledge)

    • Client confirms: "I acknowledge your sequence number (301). Let's start."

    • Contains: ACK flag set, Acknowledgment Number=301

Sequence numbers track data bytes sent. Acknowledgments confirm received bytes (ACK number = next expected byte).

Data Transfer with Reliability

During data transfer, TCP ensures reliability through:

  1. Sequencing: Each byte gets a number

  2. Acknowledgments: Receiver confirms received data

  3. Retransmission: Unacknowledged data is resent

  4. Flow Control: Prevents overwhelming the receiver

  5. Error Checking: Checksums detect corruption

Example Data Transfer:

Client: "Here's bytes 101-200" (seq=101)
Server: "Got them, send byte 201 next" (ack=201)
Client: "Here's bytes 201-300" (seq=201)

If Server doesn't acknowledge within timeout, Client retransmits bytes 201-300.

Connection Termination: Graceful Close

TCP connections close cleanly using a four-step process:

  1. FIN from one side: "I'm done sending"

  2. ACK from other side: "I acknowledge your FIN"

  3. FIN from other side: "I'm also done sending"

  4. ACK from first side: "I acknowledge your FIN"

Both sides confirm they've received all data before closing.

TCP vs. Raw Data Transfer

Without TCP:

  • No guarantee of delivery

  • No order preservation

  • No error detection

  • No flow control

With TCP:

  • All data arrives intact and in order

  • Lost packets are automatically retransmitted

  • Corruption is detected and fixed

  • Sender adapts to receiver's capacity

Why This Matters

Every web page, email, and file transfer uses TCP. When you load this page:

  1. Browser establishes TCP connection to server (3-way handshake)

  2. HTTP request sent over TCP (reliably delivered)

  3. Web page data transferred in ordered chunks

  4. Connection closes when complete

TCP's reliability makes the Internet work predictably. Without it, we would constantly worry about data loss and corruption in everyday web browsing, file downloads, and communications.

Enjoyed reading this blog? Let's connect!
🐦 Twitter/X: @rohan_gupta96
💼 LinkedIn: https://www.linkedin.com/in/rohangupta9896
🐙 GitHub: https://github.com/rohan9896

More from this blog

Rohan Gupta's Blog

14 posts