Skip to main content

TCP Working: 3-Way Handshake & Reliable Communication

Published 5 min readMarkdown

What is TCP ?

TCP(Transmission Control Protocol) is a connection oriented network protocol that ensures reliable, ordered, and error-checked delivery of data between applications on an IP network. This protocol lies in the transport layer of OSI model.

Why TCP ?

So as we know the the internet is very unreliable, unordered and stateless so make sure the data transfer is done properly and reliably without any corruption. This is why TCP is used because it provides all this features out of the box which makes the internet more predictable.

How TCP handles the data to make connection reliable

The data is transferred through the TCP in the form of packets (small data chunks) called TCP Segments. So if the data is divided into small packets then it is important to transfer all these packets properly and in a proper sequence. In order to do this some information is attached to each packet. This information is called TCP Headers. This header include information like:

  • Source Port - Sender’s Port

  • Destination Port - Receiver’s Port

  • Sequence Number - Order of data packets

  • Checksum - For checking data corruption

  • Acknowledgement Number - Confirms the received data

  • Flags - SYN, ACK, FIN flags to connection status

Importance of Sequence Number

This sequence number is responsible for the making the TCP most reliable form of connection. This sequence number helps the connection to reorder packets, detect the missing ones send the lost packets again.

After the TCP Header IP header is also attached where the source and destination IP is stored.

TCP 3-Way Handshake: SYN → SYN-ACK → SYN

To make sure both the client and server are connected and are ready to transfer the data a 3 way handshake is make which is called TCP Handshake.

  • Step 1: Client → Server (SYN)
    Here the client at first requests the server to make a connection, it stills don’t sends any data packets but adds a sequence number(x) to it - telling to start the sequence numbering from here.

  • Step 2: Server → Client (SYN + ACK)
    Here the server receives the SYN from the client and if the server wants to connect to the client it will send back packet with some data which includes:
    - SYN = 1 (Same data which was sent by client)
    - ACK = 1 (This is just a flag which tells “I have received the SYN packet“)
    - ACK Number = x(sequence number of SYN packet) + 1 → This tells “I expect the next byte to be x + 1.”
    - Sequence Number = y (Server also maintain a sequence number for the data it is going to send to the client)

  • Step 3: Client → Server (SYN)
    Now the client sends the confirmation packet as ACK=1 to tell that I have received your packet and ACK Number = y+1 tells that “I now expect the next packer from you“.

After this the three way handshake becomes successful and the client-server TCP connection is established.

How TCP ensures reliability, order, and correctness

What does reliability means ?
This means data should arrive the destination (either it is client or server) without any data loss. So make this possible TCP uses some tactics like:

  • Sequence Numbers: As we discussed every byte sent has a sequence number added to it. Lets say if the client sends 1-9 number packets. Bu the server received only 1-5 so here exactly the missing packets are resent.

  • ACKs: This become a very important which tells I have successfully received all the data between 1-9 now and the transfer was successful.

How order is maintained ?
The order i TCP means that in if the packets sent were in order 1→2→3 then it should arrive the destination in same order. While resending(retransmitting) duplicate packets also can come, so this also needs to be handled.

  • Lets take a scenario where the order received by the recipient is 3→1→2 then at the recipient side the packets are reshuffled to match the same order as it needs to be using the ACKs and sequence numbers. After this inly the packets enters the application layer of OSI model, making the whole system in order.

How correctness in maintained ?
The correctness of the packets is maintained using the checksum. On every transmit the checksum is recalculated in order to check the data is okay or not. Corrupted or duplicated data is discarded and retransmitted, guaranteeing a clean and accurate byte stream.

How a TCP connection is Closed ?

TCP closes a connection using a graceful 4-way termination handshake. Because the connection should be closed from both client and server’s side reliably without any packet loss. This is done using FIN → ACK → FIN → ACK.

  • FIN - Here comes the use FIN flag. If any one side sends this flag as FIN=1, it means it finishes sending the data but here it can receive the data so the connection half close.

  • ACK - Now the other side sends a ACK flag as ACK=1 which tells I acknowledge you connection closure.

  • FIN - Now the other side also tells I am also closing the connection and will not send any data.

  • FACK - Now the initial side says okay I got your FIN and I am acknowledging you to close the connection. Here finally the connection closed.

NOTE: After sending the final ACK the closing side enters in to TIME_WAIT in order to receive the final ACK correctly and also prevents the data from old connection to enter the new one. Making the whole thing super graceful.

About the author

  • I am a third-year undergrad student at Chandigarh University. I am into building backend and applied AI stuff using the modern technologies. I loves to document my learning through the blogs so that I can contribute to the community.

No comments yet. Be the first to comment.