Lesson 5 of 15

TCP Three-Way Handshake

TCP Three-Way Handshake

TCP (Transmission Control Protocol) establishes a reliable connection using a three-way handshake before any data is transferred.

The Handshake

Client                    Server
  │                         │
  │──── SYN (seq=x) ──────>│  Step 1: Client sends SYN
  │                         │
  │<── SYN-ACK (seq=y, ─── │  Step 2: Server sends SYN-ACK
  │     ack=x+1)            │
  │                         │
  │──── ACK (seq=x+1, ────>│  Step 3: Client sends ACK
  │     ack=y+1)            │
  │                         │
  │    CONNECTION OPEN      │

Sequence Numbers

  • SYN: The client picks an initial sequence number x
  • SYN-ACK: The server picks its own sequence number y and acknowledges x+1
  • ACK: The client acknowledges y+1

TCP States

During the handshake, the connection transitions through states:

  1. CLOSED -> SYN_SENT (client sends SYN)
  2. LISTEN -> SYN_RECEIVED (server receives SYN, sends SYN-ACK)
  3. SYN_SENT -> ESTABLISHED (client receives SYN-ACK, sends ACK)
  4. SYN_RECEIVED -> ESTABLISHED (server receives ACK)

Your Task

Implement tcpHandshake(clientSeq, serverSeq) that simulates the three-way handshake. It should return an array of 3 message objects, each with: from, to, flags (array), seq, ack.

Node.js loading...
Loading...
Click "Run" to execute your code.