Three-Way Handshake

understanding how TCP/IP networks create a connection between client and host

Understanding TCP and IP

The IP or Internet Protocol provides host to host routing and addressing for packets of data so that they can travel across networks and arrive at the correct destination. IP information is attached with those packets which helps routers in sending these packets to the right place. Every device or domain that connects to the Internet is assigned an IP address. IP addresses are the identifiers that allow information to be sent between devices on a network, they contain location information and make devices accessible for communication.

Where as TCP or Transmission Control Protocol, defines how to establish and maintain a network conversation through which application programs can exchange data, it provides an abstraction of a reliable network running over an unreliable channel which hides most of the complexity of network communication from our applications. Together, TCP and IP are the basic rules defining the Internet, TCP/IP is referred to as "Internet Protocol Suite". TCP is optimized to provide accuracy, that is, all the bytes sent will be identical to the bytes received. In practice all HTTP traffic on the Internet today is delivered via TCP due to its convenience.

Three-Way Handshake

Before Client and server exchange any data, TCP has to begin its connection using the three way handshake. Both Client and server need to agree on starting a packet sequence number (a counter used to keep track of every byte sent outward) and number of other connection specific variables. The sequence numbers are picked randomly for security.

SYN

Client picks a random sequence number x and sends a SYN packet, which may also include additional TCP flags and options.

SYN ACK

Server increments x by one, picks own random sequence number y, appends its own set of flags and options, and dispatches the response.

ACK

Client increments both x and y by one and completes the handshake by dispatching the last ACK packet in the handshake.

image.png

The application data can now begin to flow between the host and the client. The client can send data packet right after sending ACK packet, while server have to wait for ACK to arrive. This starting process applies to each TCP connection this causes performance issue because of the latency of the starting process of the connection.

For Example, if your client is in New York and host is in Sydney, to start connection, three way handshake will take minimum of 160 milliseconds, to propagate the packet in one direction. Bandwidth in this case has no role here the latency depends upon the propagation time between the client and the server.

The delay imposed by the three-way handshake makes new TCP connections expensive to create, and therefore connection reuse is a critical optimization for any application running over TCP.