DEV Community

sanjeev singh
sanjeev singh

Posted on

TCP Working: 3-Way Handshake & Reliable Communication

Introduction: The Chaos Without Rules

Imagine you're at a crowded party trying to have a conversation with someone across the room. You shout your message, but you have no idea if they heard you, if they understood you, or if your words arrived in the right order. This is essentially what happens when data is sent over a network without any rules or protocols.

When you send data over the internet—whether it's a message, a file, or a video stream—that data is broken into small pieces called packets. Without a reliable protocol, several problems can occur:

  • Packets might get lost in transit and never reach their destination
  • Packets might arrive out of order, like receiving the ending of a movie before the beginning
  • Packets might get corrupted, with bits flipped or changed during transmission
  • The sender has no confirmation that the receiver actually got the data
  • No coordination exists between sender and receiver about when to start or stop

This is where TCP comes to the rescue.

What is TCP and Why Do We Need It?

TCP (Transmission Control Protocol) is one of the core protocols of the Internet Protocol Suite. It sits at the Transport Layer and provides reliable, ordered, and error-checked delivery of data between applications running on hosts communicating over an IP network.

Think of TCP as a postal service with guaranteed delivery, tracking numbers, and receipts—as opposed to just throwing letters in the general direction of someone's house and hoping for the best.

Key Features of TCP:

  1. Reliable delivery - Ensures all data arrives
  2. Ordered delivery - Data arrives in the correct sequence
  3. Error checking - Detects and handles corrupted data
  4. Flow control - Prevents overwhelming the receiver
  5. Congestion control - Adapts to network conditions

Problems TCP is Designed to Solve

TCP addresses several critical challenges in network communication:

1. Unreliable Network Medium

The internet is built on an unreliable foundation. Routers can drop packets, network cables can introduce noise, and wireless signals can be interrupted. TCP handles all of this gracefully.

2. Packet Loss

When packets get lost in transit (due to network congestion, hardware failure, or other issues), TCP detects the loss and retransmits the missing data.

3. Out-of-Order Delivery

IP (Internet Protocol) doesn't guarantee that packets will arrive in the same order they were sent. Different packets might take different routes through the network. TCP reorders them correctly.

4. Data Corruption

Electrical interference, hardware issues, or transmission errors can flip bits in your data. TCP uses checksums to detect corruption and requests retransmission.

5. No Feedback Mechanism

Without TCP, a sender has no idea if the receiver is ready, if data arrived, or if the receiver even exists. TCP provides acknowledgments for everything.

6. Resource Management

TCP prevents fast senders from overwhelming slow receivers and helps prevent network congestion that could affect everyone.

The TCP 3-Way Handshake: Establishing a Connection

Before any data can be sent, TCP requires that a connection be established between the client and server. This happens through the famous 3-way handshake—a conversation that ensures both sides are ready to communicate.

The Handshake Analogy

Think of it like starting a phone call:

  1. You (Client): "Hello, can you hear me?" (SYN)
  2. Friend (Server): "Yes, I can hear you! Can you hear me?" (SYN-ACK)
  3. You (Client): "Yes, I can hear you too! Let's talk." (ACK)

Now both sides know the connection is working and communication can begin.

Step-by-Step: The 3-Way Handshake

Let's break down each step in detail:

Step 1: SYN (Synchronize)

Client → Server: SYN packet
Enter fullscreen mode Exit fullscreen mode

The client initiates the connection by sending a SYN (Synchronize) packet to the server.

What's in this packet:

  • SYN flag: Set to 1, indicating this is a connection request
  • Sequence number: A random initial sequence number (let's say 1000)
    • This isn't actually "1"—it's a random number for security
    • Used to track the order of bytes sent
  • Source and destination ports: Identifies the applications communicating

What this means:
"Hi Server! I want to communicate with you. I'm starting my sequence numbering at 1000."

Step 2: SYN-ACK (Synchronize-Acknowledge)

Server → Client: SYN-ACK packet
Enter fullscreen mode Exit fullscreen mode

If the server is listening and willing to accept the connection, it responds with a SYN-ACK packet.

What's in this packet:

  • SYN flag: Set to 1 (server's synchronization)
  • ACK flag: Set to 1 (acknowledging client's SYN)
  • Sequence number: Server's own random initial sequence number (let's say 5000)
  • Acknowledgment number: Client's sequence number + 1 (1001)
    • This says "I received your SYN with sequence 1000, I'm expecting 1001 next"

What this means:
"Hi Client! I received your request. I'm ready to communicate. I'm starting my sequence numbering at 5000, and I'm ready to receive your data starting at byte 1001."

Step 3: ACK (Acknowledge)

Client → Server: ACK packet
Enter fullscreen mode Exit fullscreen mode

The client sends a final ACK packet to acknowledge the server's response.

What's in this packet:

  • ACK flag: Set to 1
  • Sequence number: 1001 (client's next sequence)
  • Acknowledgment number: Server's sequence number + 1 (5001)

What this means:
"Perfect! I received your acknowledgment. I'm ready to send data starting at byte 1001, and I'm ready to receive your data starting at byte 5001. Let's communicate!"

Why 3 Steps?

You might wonder: why not just 2 steps? The third step is crucial because:

  1. It confirms to the server that the client received the SYN-ACK
  2. It allows both sides to exchange and acknowledge sequence numbers
  3. It prevents half-open connections (where client thinks it's connected but server doesn't)
  4. It protects against old duplicate connection requests from interfering

Visual Representation

Client                                Server
  |                                      |
  |  SYN (seq=1000)                      |
  |------------------------------------->|
  |                                      |
  |           SYN-ACK (seq=5000, ack=1001)|
  |<-------------------------------------|
  |                                      |
  |  ACK (seq=1001, ack=5001)           |
  |------------------------------------->|
  |                                      |
  |      Connection Established          |
  |<====================================>|
Enter fullscreen mode Exit fullscreen mode

How Data Transfer Works in TCP

Once the connection is established, actual data transmission can begin. This is where TCP really shines with its reliability mechanisms.

Sequence Numbers and Acknowledgments

Every byte of data sent over TCP is numbered using sequence numbers. This allows:

  • The receiver to know if any data is missing
  • The receiver to reorder packets that arrive out of sequence
  • Both sides to track what data has been sent and received

Example of data transfer:

Client sends 1000 bytes of data:
- Sequence number: 1001 (starting point)
- Data: bytes 1001-2000

Server acknowledges:
- Acknowledgment number: 2001
  (meaning "I received everything up to byte 2000, send 2001 next")
Enter fullscreen mode Exit fullscreen mode

Segments and Windows

TCP doesn't send all data at once. Instead:

  1. Segmentation: Data is divided into segments (chunks) that fit into network packets
  2. Window Size: The receiver tells the sender how much data it can accept at once
  3. Sliding Window: As data is acknowledged, the "window" slides forward, allowing more data to be sent

Visual representation of sliding window:

Sender's data buffer:
[Sent & ACKed][Sent, awaiting ACK][Ready to send][Not yet sendable]
              ^                    ^
              |                    |
          Left edge            Right edge
          of window            of window
Enter fullscreen mode Exit fullscreen mode

How TCP Ensures Reliability, Order, and Correctness

TCP uses several mechanisms to guarantee reliable communication:

1. Sequence Numbers (Ensuring Order)

Each byte is numbered sequentially. Even if packets arrive out of order, the receiver can reorder them correctly.

Example:

Packets sent: A (seq=1000), B (seq=2000), C (seq=3000)
Packets arrive: B, C, A (out of order)
Receiver reorders: A, B, C (correct order) ✓
Enter fullscreen mode Exit fullscreen mode

2. Acknowledgments (Confirming Receipt)

The receiver sends back acknowledgment numbers indicating which bytes have been successfully received.

Two types:

  • Cumulative ACK: "I've received everything up to byte X"
  • Selective ACK (SACK): "I've received bytes X-Y, but I'm missing Z"

3. Checksums (Detecting Corruption)

Every TCP segment includes a checksum calculated from the data. The receiver:

  1. Recalculates the checksum
  2. Compares it with the sent checksum
  3. If they don't match, the packet is corrupted and discarded
Sender calculates: checksum = f(data)
Receiver verifies: checksum == f(received_data) ?
If no: discard packet and wait for retransmission
Enter fullscreen mode Exit fullscreen mode

4. Retransmission (Handling Loss)

When packets are lost or corrupted, TCP retransmits them automatically.

How it works:

Client                           Server
  |                                 |
  | Packet A (seq=1000)            |
  |------------------------------->|
  |                          ACK (ack=2000)
  |<-------------------------------|
  |                                 |
  | Packet B (seq=2000)            |
  |--------- X  (lost!)             |
  |                                 |
  | Packet C (seq=3000)            |
  |------------------------------->|
  |                   Duplicate ACK (ack=2000)
  |<-------------------------------|
  |                                 |
  | Retransmit B (seq=2000)        |
  |------------------------------->|
  |                          ACK (ack=4000)
  |<-------------------------------|
Enter fullscreen mode Exit fullscreen mode

When retransmission happens:

  • Timeout: If no ACK received within a certain time (RTO - Retransmission Timeout)
  • Duplicate ACKs: If receiver sends the same ACK 3 times (indicates missing packet)

5. Timers

TCP uses several timers:

  • Retransmission Timer: How long to wait before retransmitting
  • Persistence Timer: Prevents deadlock when window size is zero
  • Keepalive Timer: Checks if connection is still alive during idle periods
  • TIME_WAIT Timer: Ensures old packets don't interfere with new connections

Real-World Example: Complete Data Transfer

Let's trace a complete data transfer:

1. Client wants to send "HELLO" (5 bytes)

2. Client sends:
   - Sequence: 1000
   - Data: "HELLO" (bytes 1000-1004)
   - Checksum: 0x4A2B

3. Packet travels through network
   - Might get delayed
   - Might take different route
   - Might encounter errors

4. Server receives:
   - Verifies checksum ✓
   - Stores bytes 1000-1004
   - Sends ACK: 1005 (expecting byte 1005 next)

5. Client receives ACK:
   - Knows "HELLO" arrived safely ✓
   - Can send next data if any
   - If no ACK received → retransmit after timeout
Enter fullscreen mode Exit fullscreen mode

How a TCP Connection is Closed

Just like establishing a connection requires a handshake, closing a connection requires a proper termination sequence. This is called the 4-way handshake for connection termination.

Why Proper Termination Matters

  • Ensures all data has been sent and received
  • Prevents data loss
  • Releases resources properly
  • Allows both sides to close gracefully

The 4-Way Termination Process

Step 1: FIN from Client

Client → Server: FIN packet
Enter fullscreen mode Exit fullscreen mode

The client (or server) that wants to close the connection sends a FIN (Finish) packet.

What it means:
"I'm done sending data, but I can still receive."

Step 2: ACK from Server

Server → Client: ACK packet
Enter fullscreen mode Exit fullscreen mode

The server acknowledges receipt of the FIN.

What it means:
"I understand you're done sending. Let me finish my data transmission."

Step 3: FIN from Server

Server → Client: FIN packet
Enter fullscreen mode Exit fullscreen mode

When the server is also done sending data, it sends its own FIN.

What it means:
"I'm also done sending data now."

Step 4: ACK from Client

Client → Server: ACK packet
Enter fullscreen mode Exit fullscreen mode

The client acknowledges the server's FIN.

What it means:
"Acknowledged. Connection is now fully closed."

Visual Representation of Connection Closure

Client                              Server
  |                                    |
  |  FIN (seq=X)                       |
  |----------------------------------->|
  |                                    | (Server can still send data)
  |               ACK (ack=X+1)       |
  |<-----------------------------------|
  |                                    |
  |               FIN (seq=Y)         |
  |<-----------------------------------|
  |                                    |
  |  ACK (ack=Y+1)                    |
  |----------------------------------->|
  |                                    |
  |   Connection Closed               |
Enter fullscreen mode Exit fullscreen mode

TIME_WAIT State

After sending the final ACK, the client enters a TIME_WAIT state (typically 2 × MSL, Maximum Segment Lifetime, usually 2-4 minutes).

Why?

  • Ensures the final ACK reaches the server
  • Prevents old duplicate packets from interfering with future connections
  • Allows all packets from this connection to expire from the network

Half-Close

TCP supports "half-close" where one side finishes sending but still receives:

Client sends FIN → Can't send anymore, but can still receive
Server sends ACK → Can continue sending data to client
Later, Server sends FIN → Now both sides are done
Enter fullscreen mode Exit fullscreen mode

This is useful for protocols like HTTP where the client sends a request and then only needs to receive the response.

TCP Connection Lifecycle: Putting It All Together

Let's visualize the complete lifecycle of a TCP connection:

┌─────────────────────────────────────────────────────────┐
│              TCP CONNECTION LIFECYCLE                    │
└─────────────────────────────────────────────────────────┘

1. ESTABLISHMENT (3-Way Handshake)
   ═══════════════════════════════════
   Client          Server
     |    SYN        |
     |-------------->|
     |   SYN-ACK     |
     |<--------------|
     |     ACK       |
     |-------------->|
     |               |
   ✓ Connected!

2. DATA TRANSFER
   ══════════════════════════════════
     |  Data (seq)   |
     |-------------->|
     |    ACK        |
     |<--------------|
     |  Data (seq)   |
     |<--------------|
     |    ACK        |
     |-------------->|
     |               |
   (Continues...)

3. TERMINATION (4-Way Handshake)
   ══════════════════════════════════
     |     FIN       |
     |-------------->|
     |     ACK       |
     |<--------------|
     |     FIN       |
     |<--------------|
     |     ACK       |
     |-------------->|
     |               |
   ✓ Closed!
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  1. TCP is essential for reliable internet communication—without it, we'd have chaos
  2. The 3-way handshake establishes a connection and synchronizes sequence numbers
  3. Sequence numbers and ACKs ensure data arrives in order and without loss
  4. Retransmission and checksums handle packet loss and corruption
  5. Proper connection termination ensures clean closure and resource release
  6. TCP trades speed for reliability—it's perfect for applications where accuracy matters more than raw speed (web browsing, email, file transfers)

When NOT to Use TCP

While TCP is amazing, it's not always the right choice:

  • Real-time applications (video calls, online gaming): UDP is better—occasional packet loss is acceptable
  • Broadcasting/Multicasting: TCP is one-to-one only
  • Small, simple requests: The overhead of establishing a connection might not be worth it

Conclusion

TCP is the unsung hero of the internet. Every time you load a web page, send an email, or download a file, TCP is working behind the scenes to ensure everything arrives safely and in order.

The 3-way handshake might seem like extra overhead, but it's the foundation that makes reliable internet communication possible. The combination of sequence numbers, acknowledgments, checksums, and retransmissions creates a robust system that handles the chaos of the internet gracefully.

Understanding TCP gives you insight into how the internet actually works and helps you debug network issues, optimize application performance, and appreciate the engineering that goes into every data transfer.


Top comments (0)