DEV Community

Cover image for TCP’s Trust Issues: A 3-Way Handshake Story
Kunal
Kunal

Posted on

TCP’s Trust Issues: A 3-Way Handshake Story

You send a message

You wait

Still no reply

Left on read you think ?

But thats not the case someone is between you and her that wants acknowledgment first

No its not her bff

Its TCP...

So... In this blog we will discuss :

  • What are data packet and what it contains ?

  • What is TCP and why is needed ?

  • Problems TCP is designed to solved

  • What is the TCP 3-way Handshake ?

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

  • How data trasfer works in TCP ?

  • How a TCP connection is closed ?


Data Packets

Think of you , sending a long message to your friend on whatsapp .

So behind the scene the internet breaks your message into small peices and send it seperately and it will ressemble them at the recieivers side

👉 A data packet is small unit of data in which your message is broken before treaveling over a network

If you dont know about network i recommend you to read this blog first 👇

Now lets see how a data packet look like

data paket

It consist of three components :

  1. Header :- It is the travel guide of a packet 🙎‍♂️ . It contains :
  • Sender IP address ( from where )

  • Reciever IP address ( to where )

  • Packet number ( order )

  • Protocol info ( TCP / UDP )

  1. Payload :- It is the actual data what you want to send .

  2. Trailer :- It is the method of identifying the end of packet . Also , some form of error checking to ensure packet is arrives at the recievers end .

Now message is sent to your friend . But there are some rules that these data packets follow or we can say some protocols


TCP : Transmission Control Protocol

Rohan wants to send a invitation letter to his friend vikas

Before sending anyting he calls his friend

Rohan :- Are you home tommorow ?
Vikas :- Yes , why ?
Rohan :- Sending you a letter

Connection established

If vikas says no i am not free then rohan dont send utill vikas confirms

👉 TCP is a protocol ( a set of rule ) that :

  • Allows devices to communicate over a network .

  • Establishes a connection between sender and receiver before data transmission begins .

  • It ensure that data reaches correctly at its destination in right order even if the network is slow .

  • It is slow as compare to UDP (Uer datagram protocol) but ensure that there is no data packet loss between communication .

It is slow but that doesn't mean its not usefully both are usefull at its own usecase

What does TCP do if the packet is missing or out of order ?

If missing/lost
TCP send the missing packets back to the sender and ask it to send the packet again

If Out of order
TCP wait , reorder them , before delivering the data

Problems TCP is designed to solved

TCP solves reliabilty and guaranteed deliver of data packets to the reciever.

Problem solved right ? But how the TCP is so sure that the data packet reaches at the reciever correctly without any missing or out of order 🤔


TCP : 3-way Handshake

SYN :- Synchronize, A request sends by sender to receiver to ask if communication can begin .
SYN-ACK :- A response confirming the request was received and the receiver is ready .
ACK :- Acknowledgment , its is the confirmation send by sender that TCP is connected .

3way

TCP three-way handshake is nothing its just each packet has to be acknowleged as delivered by recipient .

  • First , the sender sends a packet . It like knocking on your friend door.
    Your know the address but you are not sure if your friend is at home . It is SYN

  • Second , Your friend reply with SYN-ACK . "I am here , come in" . It means your friend receives the packet .

  • Finally , the sender send ACK . "Okay , connection established . Now we can talk " . It means the connection is build between sender and receiver and no data packet can loss now

TCP connection successfull . Now how data transfer works in this


How does data transfer in real life

  • TCP tries to connect or established the network between sender and receiver via three - way handshake .

  • If the connection is successfull your data (image , message etc) is break down into chunks of packet called data packet .

  • Now , these data packets are send sequentially with their number .

  • Receiver sends an acknowledgment that they receive these data packets .

  • Lost packets resent by the sender , out of order packets are rearranged

  • After all the data is delivered . TCP closed its connection .

Once all the data is deliverd TCP also define how a connnection is safely terminated .


TCP Termination : 4-way handshake

Closing a TCP connection requires a four-way handshake to ensure that both side finish transmitting data safely .

tcp close

  • FIN (Finish) :- First , The sender who wants to close their connection send a FIN segment to the receiver . It is ready to close connection and waiting for the receiver .

  • ACK (Acknowledge) :- Now this FIN receives by the receiver and acknowledge with ACK .

  • FIN from receiver :- The receiver then send its own FIN to sender that it is also ready to close the connection .

  • ACK :- The sender reponds with an ACK , completing the termination .

This ensures that all remaining data is transmitted before the connection is fully close


Thanks for reading ! if enjoyed this blog , you can read more on this 👇

Top comments (0)