Skip to main content

Why Isn't a 2-Way Handshake Enough in TCP?

🎯 The Question​

"Why does TCP need 3 packets (SYN, SYN-ACK, ACK) to establish a connection? Why isn't a 2-way handshake (SYN, SYN-ACK) enough?"


⚑ 30-Second Elevator Pitch​

In a 2-Way Handshake, the client sends a SYN ("I want to connect"), and the server replies with SYN-ACK ("I am ready to connect").

At this point, the client knows the server is alive and ready, but the server has no confirmation whether its reply ever reached the client.

To prevent half-open / phantom connections and ensure mutual confirmation from both sides, TCP requires a third packetβ€”the final ACK from the client. Only after this packet is received is the full-duplex connection marked ESTABLISHED.


🧠 Deep Dive: Bidirectional Confirmation & Sequence Sync​

1. Step-by-Step Packet Exchange & Sequence Synchronization​

To establish a reliable bidirectional channel, both endpoints must synchronize their independent 32-bit sequence numbers (XX and YY):

  • Step 1 β€” SYN (seq = X) [Client β†’\to Server]:
    The Client initiates the connection by choosing an initial sequence number (XX).
    What is verified: Server learns that the Client can send data.

  • Step 2 β€” SYN + ACK (seq = Y, ack = X + 1) [Server β†’\to Client]:
    The Server acknowledges the client's sequence number (X+1X + 1) and transmits its own initial sequence number (YY).
    What is verified: Client confirms the Server can receive and send data. However, the Server is still unconfirmed if its reply ever reached the Client.

  • Step 3 β€” ACK (ack = Y + 1) [Client β†’\to Server]:
    The Client confirms receipt of sequence number YY by acknowledging (Y+1Y + 1).
    What is verified: Server now knows the Client can receive data. Both sequence numbers (XX and YY) are mutually synchronized, and the connection is officially ready for full-duplex transmission.


πŸ’‘ What Interviewers Ask Next (Follow-Up Traps)​

  1. "What happens if the 3rd ACK packet gets lost?"

    • Answer: The server will not transition to ESTABLISHED immediately. It will retransmit the SYN-ACK packet after a timeout. If the client tries to send data immediately, the first data packet will also carry the ACK flag, completing the connection implicitly.
  2. "Can delayed old duplicate SYN packets cause ghost connections in a 2-way handshake?"

    • Answer: Yes. In a 2-way handshake, if an old delayed SYN packet arrives at the server, the server would immediately allocate memory buffers and open a connection for a client that might not even exist anymore. The 3-way handshake prevents this because the client will reject stale connection attempts with an RST (Reset) packet.

πŸ“Œ Summary Cheatsheet​

Property2-Way Handshake (Insufficient)3-Way Handshake (TCP Standard)
Client Confirmationβœ… Knows server is reachableβœ… Knows server is reachable
Server Confirmation❌ Cannot confirm if reply reachedβœ… Confirms client received response
Connection Safety❌ Vulnerable to ghost/half-open stateβœ… Mutual synchronized state
Final StateAmbiguous server stateESTABLISHED on both endpoints
Placement Takeaway

Interview Answer: TCP uses a 3-way handshake because a 2-way exchange leaves the server unconfirmed about whether its reply reached the client. The final ACK guarantees bidirectional confirmation and prevents phantom/half-open connections on the server.


πŸ“Ί Video Explanation​

πŸ’¬

Discussion & Doubts