Skip to main content

Command Palette

Search for a command to run...

TCP Working: 3-Way Handshake & Reliable Communication

Updated
6 min read
TCP Working: 3-Way Handshake & Reliable Communication
M
hi , I am a computer science student learning web development and writing blogs on the topics which I have been learning . Hope you like it !

The Networking protocols

we know that for the transmission of data over the internet we have to follow some protocols which are known as the Networking protocols they tell us how data should be send , format and received across the internet.

there are many different networking protocols and one of them is TCP ( Transmission Control Protocol ) which play a very vital role in transmitting the data.

TCP ( Transmission Control Protocol )

TCP is one of the most reliable protocol i.e it ensures that the data is sent from one end point to another end point without being lost , damaged and duplicate .

In this the data is transfer in a proper order and if there is any chance of loss of data then it is detected and retransmitted

Real life Example of TCP

it is mainly used in the situation where we want the data to be secured , organized and without damages

where the data itself is more important then the speed of transmission of data .

example of TCP are

  1. web browsing , email, downloading videos , images or files etc.

3 Way handshake

this 3 way handshake is like a process which is done to build a connection between the server and client before transmitting the actual data .

this process takes place in 3 parts

  1. SYN ( Synchronize sequence Number)

  2. SYN+ ACK ( Synchronize sequence Number + Acknowledgement )

  3. ACK ( Acknowledgement)

let's understand these 3 steps one by one

SYN ( Synchronize sequence Number)

In this step a segment is send to the server with a sequence number generated by the client . this is only to make sure whether the server is available or not .

SYN + ACK

this step the server generate an acknowledgement which ensures that the data is being received by it .

ACK

this is the final step by the client which ensures the a reliable connection is built between the client and the server and now they are ready for the transmission of data.

imagine this process as a real life conversation

SYN : ( client -> server )

Hi , i want to connect with you .

SYN + ACK : ( server -> client)

yes i get your request and can connect with you

ACK : ( client - > server)

ok , let's start talking

How data transfer works in TCP

the data is send into small packets which are known as segments these segments consists of 2 parts

  1. header

  2. payload

the header contains the information such as

  • source port address

  • destination number address

  • checksum

  • sequence number

  • acknowledgement number

  • control flag

after building a reliable connection by the 3-way handshake the data packets are send from the source to the destination and as the receiver send the acknowledgement for every received packets so even if the data is lost then it is retransmitted by the sender .

How TCP ensures reliability order, and correctness.

ordering : as we have seen earlier that inside the header of the every segment we have a sequence number for every data that helps us to arrange the data into sequence .

error detection : TCP uses a checksum technique for error detection

let see how this works

on sender end :

  • In this checksum technique our data is converted into a 16 bit words

  • then these words are added and their one's complement are taken.

  • we place this result in the checksum field of the header and then the data is send to the server

On receiver 's end

  • the same calculation is done and the checksum is calculated

  • then the server matches the calculated value with sender's checksum value

  • if they matches then the data is safe and correct

  • if don't then the data is corrupted , in this case the receiver do not send any ACK to the sender

  • and when no ACK arrive to the sender then it retransmit the data to the receiver

reliability : the TCP ensures reliability using the ACK and retransmission

whenever receiver get the segment it send an ACK to the sender ensuring that the data is received .

if no ACK is received at the server end , then it assume that the data is lost somewhere , it automatically retransmit the segment

How a TCP connection is closed

A TCP connection is closed by a 4-way handshake method just like the 3-way handshake contains 3 major steps similarly this contains the 4 major steps to closed the connection between the sender and receiver

FIN ( sender -> receiver)

the sender first send a segment with FIN ( finish) flag set , this indicates that all the data is transmitted and now the sender wants to closed the connection .

ACK : ( receiver -> sender)

once receiving the FIN segment by the receiver , it first send an ACK which shows the acceptance of request made by the sender .

FIN : ( receiver -> sender)

after transmitting its remaining data ,the receiver itself send its own FIN segment showing its end of connection ( now the receiver is also ready to close the connection)

ACK : ( sender -> receiver)

this is the end acknowledgement made by the sender to acknowledge the receiver request and fully closed the connection

imagine this situation as a phone call between the sender and receiver

sender : FIN

all work is done , now i want to end the connection

receiver : ACK

okk , i got your request

receiver ( after finishing its work ) FIN

i am done too, lets end the connection

sender : ACK

alright , Goodbye !

connection ends successfully !


So this was all for today , hope you enjoy reading this . Happy learning ! 🎉