DEV Community

Hiral
Hiral

Posted on

TCP Working: 3-Way Handshake & Reliable Communication

Imagine you are sending letter to a friend and you decide break into pieces and send it separately, how does your friends know to keep in right order, if any of the piece get lost.
Computer faces same challenge while sending data over internet. TCP is most reliable communication possible.

Let understand TCP:

When data is transferred over internet it is broken into small pieces and called packets. TCP ensures:

  • All packets are transferred
  • Packet arrive the destination correctly
  • Data isn't corrupted during transmission
  • Lost packets are reset automatically

TCP 3-way handshake:
Before sending data TCP establishes 3-way handshake
consider your talking on phone:

  • you say "hello"
  • person other side " hello"
  • you say"ok let's talk"

Step1: SYN - computer sends synchronize message with starting number let's say 100
Step2:SYN-ACK -Server responds " I got your number 100" and response is "300"
ste3: ACK - computer confirms that is got the response

Data is broken in pieces and TCP keeps track of each of pieces using sequence number.

How does TCP handles any problem:

  • Lost packet: if packet doesn't arrive ,TCP waits a bit then sends it again
  • Wrong order: TCP uses the sequence number to organize the pieces that are out of order
  • Corrupted data: TCP includes checksum to detect any errors and request a fresh copy This how downloads complete successfully.

How does TCP close connection:
4 way handshake
When data transfer is done, TCP politely close the connection:

Computer : FIN : I am done
Server: ACK :Okay
Server: I am done too
Computer: ACK : Goodbye
Both the sides confirm they're finished, this prevents data loss.

Why TCP is important:
Every time browser a request, TCP works behind the scene:
It makes sure

  • Data arrives reliable
  • Connection starts 3 way handshake
  • Data is tracked with sequence of number
  • Lost or corrupted data is automatically resend
  • Connection close with a 4- way handshake

Top comments (0)