TCP 3-Way Handshake Animation
Interactive Visualization of Connection Establishment and Teardown
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.
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
- Step-by-Step: Click “Next Step” to advance one packet at a time
- Auto-Play: Click “Auto Play” to watch the full sequence
- Teardown: Switch to “Connection Teardown” mode to see FIN sequence
- Timeout Demo: Click “Simulate Timeout” to see retry behavior
- 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) ====|
- 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:
- FIN (Client -> Server): Client initiates close
- ACK (Server -> Client): Server acknowledges
- FIN (Server -> Client): Server closes its side
- ACK (Client -> Server): Client acknowledges, enters TIME-WAIT
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:
- SYN sent but gets lost in the network
- RTO (Retransmission Timeout) expires (typically 1-3 seconds)
- Retry with exponential backoff: 1s, 2s, 4s, 8s, 16s, 32s…
- Maximum retries: Usually 5-6 attempts before giving up
- Final result: Connection established or
ETIMEDOUTerror
- 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
- 3-Way Handshake: SYN -> SYN-ACK -> ACK sequence for reliable connection
- State Transitions: Both client and server change states through the process
- Sequence Numbers: How seq and ack numbers establish synchronization
- 4-Way Teardown: Graceful connection close with FIN packets
- Timeout Handling: What happens when packets are lost
- UDP Comparison: Zero-RTT connectionless alternative
Conceptual Diagrams
What’s Next
- Transport Fundamentals - Deep dive into TCP/UDP
- Transport Optimizations - TCP optimizations for IoT
- DTLS Security - Securing UDP with DTLS
- CoAP Protocol - UDP-based IoT protocol
- MQTT Animation - See application-layer protocol flow
- Simulations Hub - More interactive visualizations
This animation is implemented in ~500 lines of Observable JavaScript:
Key Techniques:
- SVG-based graphics: Scalable, accessible, print-friendly
- D3 transitions: Smooth packet animations with
d3.easeQuadInOut - State machine: Tracks current step, mode, and connection states
- Multiple modes: Handshake, teardown, timeout, UDP comparison
- IEEE colors: Consistent palette (#2C3E50, #16A085, #E67E22)
Animation Flow:
nextStep() -> animatePacket() -> drawStaticMessage() -> updateUI()
Mode Switching:
handshake: Standard 3-way handshaketeardown: 4-way connection closetimeout: Lost SYN with retryudp: Connectionless comparison