TCP 3-Way Handshake Animation

Interactive Visualization of Connection Establishment and Teardown

animation
tcp
protocols
transport
In 60 Seconds

TCP establishes reliable connections through a 3-way handshake: the client sends SYN with its initial sequence number, the server responds with SYN-ACK containing its own sequence number and acknowledging the client’s, and the client confirms with ACK. This 1.5-RTT setup cost is why UDP-based protocols like CoAP are preferred for constrained IoT devices that need minimal connection overhead.

TCP Connection Establishment

This interactive animation demonstrates how TCP establishes reliable connections using the 3-way handshake. Watch as the Client and Server exchange SYN, SYN-ACK, and ACK packets with sequence numbers, and observe state transitions on both sides.

Animation Overview

This animation shows:

  • 3-Way Handshake: SYN -> SYN-ACK -> ACK sequence
  • State Transitions: CLOSED -> SYN-SENT -> ESTABLISHED (client) and LISTEN -> SYN-RECEIVED -> ESTABLISHED (server)
  • Sequence Numbers: Real seq/ack number exchanges
  • Connection Teardown: FIN sequence for graceful close
  • Timeout & Retry: What happens when SYN is lost
  • UDP Comparison: See why UDP doesn’t need a handshake
How to Use This Animation
  1. Step-by-Step: Click “Next Step” to advance one packet at a time
  2. Auto-Play: Click “Auto Play” to watch the full sequence
  3. Teardown: Switch to “Connection Teardown” mode to see FIN sequence
  4. Timeout Demo: Click “Simulate Timeout” to see retry behavior
  5. Compare UDP: Toggle to see UDP’s connectionless approach

TCP vs UDP Comparison

The animation above lets you compare TCP and UDP side by side. Here’s a summary:

Feature TCP UDP
Connection Setup 3-way handshake (SYN, SYN-ACK, ACK) None - connectionless
Round Trips Before Data 1.5 RTT (minimum) 0 RTT
Reliability Guaranteed delivery, ordering Best-effort, may lose packets
Flow Control Sliding window None
Use Cases HTTP, SSH, file transfer DNS, VoIP, gaming, video
IoT Relevance MQTT over TCP, HTTP REST CoAP over UDP, DTLS

Sequence Number Mechanics

Understanding the sequence numbers in the handshake:

Client (ISN=100)                    Server (ISN=300)
    |                                    |
    |-------- SYN seq=100 ------------->|
    |                                    |
    |<------ SYN-ACK seq=300 ack=101 ---|
    |        (ack = client_seq + 1)      |
    |                                    |
    |-------- ACK seq=101 ack=301 ----->|
    |        (ack = server_seq + 1)      |
    |                                    |
    |===== ESTABLISHED (both sides) ====|
Why Sequence Numbers Matter
  • ISN (Initial Sequence Number): Randomly chosen to prevent spoofing
  • ACK number: “I’ve received everything up to this byte, send next”
  • SYN consumes 1 sequence number: That’s why ack = seq + 1
  • Security: ISN randomization prevents TCP sequence prediction attacks

State Transition Diagram

This decision tree helps determine when TCP or UDP is more appropriate for IoT applications.

Connection Teardown (4-Way Close)

Click the “Teardown” button in the animation to see:

  1. FIN (Client -> Server): Client initiates close
  2. ACK (Server -> Client): Server acknowledges
  3. FIN (Server -> Client): Server closes its side
  4. ACK (Client -> Server): Client acknowledges, enters TIME-WAIT
TIME-WAIT State

The client enters TIME-WAIT for 2 x MSL (Maximum Segment Lifetime, typically 2 minutes):

  • Ensures delayed packets don’t corrupt new connections
  • Allows retransmission of final ACK if lost
  • Important for server reliability

Timeout and Retry Behavior

Click “Timeout Demo” to see what happens when a SYN packet is lost:

  1. SYN sent but gets lost in the network
  2. RTO (Retransmission Timeout) expires (typically 1-3 seconds)
  3. Retry with exponential backoff: 1s, 2s, 4s, 8s, 16s, 32s…
  4. Maximum retries: Usually 5-6 attempts before giving up
  5. Final result: Connection established or ETIMEDOUT error
IoT Implications
  • Battery drain: Retries consume significant power on constrained devices
  • Latency: Each retry adds seconds to connection time
  • UDP alternative: CoAP over UDP with application-layer reliability may be better

What This Animation Demonstrates

  1. 3-Way Handshake: SYN -> SYN-ACK -> ACK sequence for reliable connection
  2. State Transitions: Both client and server change states through the process
  3. Sequence Numbers: How seq and ack numbers establish synchronization
  4. 4-Way Teardown: Graceful connection close with FIN packets
  5. Timeout Handling: What happens when packets are lost
  6. UDP Comparison: Zero-RTT connectionless alternative

Conceptual Diagrams

What’s Next


This animation is implemented in ~500 lines of Observable JavaScript:

Key Techniques:

  1. SVG-based graphics: Scalable, accessible, print-friendly
  2. D3 transitions: Smooth packet animations with d3.easeQuadInOut
  3. State machine: Tracks current step, mode, and connection states
  4. Multiple modes: Handshake, teardown, timeout, UDP comparison
  5. IEEE colors: Consistent palette (#2C3E50, #16A085, #E67E22)

Animation Flow:

nextStep() -> animatePacket() -> drawStaticMessage() -> updateUI()

Mode Switching:

  • handshake: Standard 3-way handshake
  • teardown: 4-way connection close
  • timeout: Lost SYN with retry
  • udp: Connectionless comparison