Skip to main content

Command Palette

Search for a command to run...

Working of TCP: 3-Way Handshake

Updated
5 min read
Working of TCP: 3-Way Handshake

What is TCP and why it is needed

TCP is short for Transmission Control Protocol. TCP is a very reliable form of communication between two devices and it ensures the entire data is transferred. In TCP packets are sent in a numbered order, which helps in retransmission of missing packets. It does not prevent packet loss but recovers from it. It is used for important tasks like downloading/uploading data, webpages, etc. TCP is like sending a courier to a friend’s address, this ensures the entire data reaches the person safely, although it is slow.

TCP is used when reliable, ordered, error-checked delivery is needed in a network. It is used when a slight delay can be tolerated but correctness is the priority over speed. If TCP were not used, users might notice missing or corrupt data. When applications are expected guaranteed delivery, TCP is used. TCP is needed because it is made assuming the network is unreliable and faces issues like packet loss, connection drop, slowing down and rerouting traffic mid conversation. All of the packets do not take the same path, some routes might be fast while some might be slow changing the order in which packets were sent. They might be sent in the order 1→2→3→4→5 but are received in the order 4→5→2→1→3. TCP reorders the packets before sending them to the application.

Problems TCP is designed to solve:

  • Packet loss:
    Data packets can be lost during transit due to congestion or unreliable network.

  • Out of order delivery:
    Due to problems like packet loss, connection drop and rerouting traffic, packets are received in an out of order manner, meaning they are sent in a different order than the order they are received in.

  • Unreliable network paths:
    Routes can be changed mid-communication, causing delay and drops.

  • No reliability in IP
    The network does not guarantee delivery or correctness.

What is the TCP 3-Way Handshake

3-Way Handshake is the method TCP uses to confirm the connection before transferring the data starts. In this process, the client sends a SYN (Synchronization) message to the server. Once the server receives the SYN message, it sends back SYN-ACK(Synchronization-Acknowledgement) to the client signaling that the SYN was received and acknowledging. When the client receives back the SYN-ACK, it knows that the server is active and then the client send back an ACK(Acknowledgement) telling the server that the transfer can begin now, since the connection has been established. After this 3 way handshake containing SYN, SYN-ACK and ACK, continuous data/packet transfer starts. This exchange ensures that both sides are ready for reliable communication.

A great analogy to explain this is during an online meeting, when a person comes forward to speak. Their first words are “Am I audible?” which is similar to the client sending SYN. After this, the listeners signal them back telling them that they are audible, which is SYN-ACK in our case. This interaction is followed by the speaking saying something indicating that they will begin speaking, exactly the way client sends back an ACK before the data transfer starts.

Step-by-step working of SYN, SYN-ACK, and ACK

  • Step 1: SYN

    The client send the SYN message to the server asking if it is ready to communicate

  • Step 2: SYN-ACK

    The server sends back SYN-ACK to the client signaling that it is indeed ready for communication.

  • Step 3: ACK

    The client sends back ACK to the server, telling the server that the transfer can begin now, since the connection has been established.

How data transfer works in TCP

The data transfer process in TCP works by breaking down the data into smaller segments. Each of these segments is sent separately. These segments are called packets. Each segment is given a sequence number. These segments are shared/transferred in a sequential manner, ensuring that the receiver gets them in the same order as they were sent. These sequence numbers are useful to tell when a certain packet has be lost during the transit. In TCP receivers send ACKs(Acknowledgement) to the sender to confirm received data. This is how TCP knows what arrived safely. If receiver doesn’t send acknowledgement (ACK) for a certain packet, then it is concluded that it went missing and sender retransmits the segment to the receiver. Even if packets are received in an un-ordered manner, TCP reorders them before they reach the application.

How a TCP connection is closed

When two computers are talking using TCP, both sides can send data. So, if one side is done sending the data, the other side might still have data left to send. If we just cut the connection, the last data might be lost and the other side might still be sending data leading to application getting incomplete results.

This is when FIN and ACK comes into play, FIN signals the computer that “Hey, I am done sending data” and ACK means “I understood that”

Step by step: How a TCP connection is closed:

  • Step 1: FIN

    One side(assume client) sends a FIN packet, telling the server that I have no more data to send you. This means the client cannot send more data after this but the server can still send data.

  • Step 2: ACK
    The server sends an ACK, telling the client that “I have received your FIN”
    Client-server connection is closed but server-client is still open.

  • Step 3: FIN

    When the server is also done sending the data, it send back its own FIN.

  • Step 4: ACK

    The client send the final ACK to the server, telling “I have received your FIN”

    This is when the entire connection is closed and no more data can be shared and both the sides are done.

Conclusion:

TCP exists because networks are unreliable and applications should not have to deal with packet loss, reordering or partial data.

From the 3-way handshake to reliable data transfer and graceful connection termination, TCP ensures communication is complete, ordered and correct.

This is why TCP is used for critical tasks like web browsing, file transfers and APIs is where correctness matters more than speed.

More from this blog

The Build Log

14 posts