CodePark/ questions / TCP connections in depth
START PRACTICINGPRACTICE
Backend question · OS, networking & Linux › TCP connections in depth

Describe the TCP three-way handshake. Why three messages?

TCP opens a connection with three segments (TCP packets) before any data flows:

  • The client sends a SYN. It carries the client's initial sequence number.
  • The server replies with a SYN-ACK. It carries the server's own initial sequence number and acknowledges the client's number.
  • The client sends an ACK that acknowledges the server's number. The connection is now open on both sides.

A sequence number counts bytes, so each side can track what was sent and received. Three messages is the minimum for each side to announce its starting number and confirm the other side received it. The initial sequence numbers are random. This protects against old duplicate segments and spoofed (faked) packets.

The handshake also agrees on options, such as:

  • maximum segment size (MSS)
  • window scaling
  • selective acknowledgements

The cost is one full round trip before the client can send data. This is why connection reuse and pooling matter. TLS setup on top of TCP adds more delay.

  • The three messages are SYN, SYN-ACK and ACK.
  • Both sides send their initial sequence numbers and acknowledge the other's.
  • The handshake agrees on options like MSS and window scaling.
  • It costs one round trip before data can flow, so connections should be reused.