Skip to content
We areTOP 1%

Explain the TCP three-way handshake.

MediumConceptTCP & UDP
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

Answer

Before sending data, TCP opens a connection with three messages: SYN, SYN-ACK, ACK. The handshake confirms that both sides are ready and exchanges their initial sequence numbers (ISNs), the random starting points used to number every byte each side sends.

  • SYN (synchronise): the client sends its ISN, say x. SYN-ACK: the server sends its own ISN y and acknowledges with ack = x + 1, meaning "I expect byte x + 1 next". ACK: the client replies with ack = y + 1, and both directions are open.
  • Three messages are the minimum because each side must send its own ISN and see it acknowledged. With only two, the server could not know that its ISN arrived, and delayed duplicate SYNs could open stale connections.
  • A SYN flood attack sends many SYNs and never completes the handshake, filling the server's queue of half-open connections; SYN cookies defend against it.

Example:

Client                                    Server
  | --- SYN      seq=1000 ---------------->  |
  | <-- SYN-ACK  seq=5000, ack=1001 -------  |
  | --- ACK      seq=1001, ack=5001 ------>  |
  | --- data     bytes 1001-1100 --------->  |

The SYN consumes one sequence number, so the client's first data byte is number 1001.

More in TCP & UDP

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account