Woodstock Blog

a tech blog for general algorithmic interview questions

[Design] TCP 3-Way Handshake

Handshaking

Handshaking is an automated process of negotiation that dynamically sets parameters of a communications channel established between two entities before normal communication over the channel begins.

It is usually a process that takes place when a computer is about to communicate with a foreign device to establish rules for communication.

TCP three-way handshake

TCP three-way handshake is the method used by TCP set up a TCP/IP connection over an Internet Protocol based network.

It’s commonly referred to as “SYN-SYN-ACK”.

Process

  1. Host A sends a TCP SYNchronize packet to Host B
  2. Host B receives A’s SYN
  3. Host B sends a SYNchronize-ACKnowledgement
  4. Host A receives B’s SYN-ACK
  5. Host A sends ACKnowledge
  6. Host B receives ACK.
  7. TCP socket connection is ESTABLISHED.

Alternatively, there’s a good illustration on wiki:

Establishing a normal TCP connection requires three separate steps:

  1. The first host (Alice) sends the second host (Bob) a “synchronize” (SYN) message with its own sequence number x, which Bob receives.

  2. Bob replies with a synchronize-acknowledgment (SYN-ACK) message with its own sequence number y and acknowledgement number x + 1, which Alice receives.

  3. Alice replies with an acknowledgment message with acknowledgement number y + 1, which Bob receives and to which he doesn’t need to reply.

Two more thing

Note that FTP, Telnet, HTTP, HTTPS, SMTP, POP3, IMAP, SSH and any other protocol that rides over TCP also has a three way handshake performed as connection is opened.

TCP ‘rides’ on top of Internet Protocol (IP) in the protocol stack. IP handles IP addressing and routing and gets the packets from one place to another, but TCP manages the actual communication sockets between endpoints.