TCP & UDP

Full form-
TCP (Transmission Control Protocol)
UDP (User Datagram Protocol)

Why do we have 2 different transport protocols?


  • TCP is a reliable protocol.
  • UDP is a unreliable or best-effort protocol.
Comparing reliable and unreliable, certainly our answer will be reliable connectivity , we wanna make sure that our data is received successfully .
But, this is not the answer for all the cases.
TCP is reliable but comes with more overhead then UDP and since UDP  have less overhead so it can transfer packets much faster than TCP.

Let's take some examples to understand the need of TCP and UDP.

In our first example, We are transferring $100 from our bank account to our brother's account, through online banking since we are in rush (imaginary condition - we can choose TCP and UDP) we have chosen UDP for fast transfer speed but something went wrong some packets lost in the mid of the transaction then what can we do we have lost out $100.
That's where we need a reliable connection, we want acknowledgement that $100 have been transferred to brother's account, we don't want our money to loss somewhere in air, we want reliability here so we need TCP. TCP goes through three-way handshake to make a reliable connection and make sure the $100 have reached to brother. In case some of the IP

packets don’t make it to your computer/mobile you want to make sure this transaction will be retransmitted from our computer to bank and from my account to brother's account.

3-way handshake-


STEP 1: Host A sends the initial packet to Host
B. This packet has the "SYN" bit enabled. Host
B receives the packet and sees the "SYN"

STEP 2: Assuming Host B has enough resources, it sends a packet back to Host A and with the "SYN and ACK".

STEP 3: After all that, Host A sends another packet to Host B and with the "ACK" bit set (1), it
effectively tells Host B 'Yes, I acknowledge your previous request'.



In our second example we are the network engineer for a major company and we just told our boss
how awesome this brand new open source Voice over IP solution is. We decide to implement this
new VoIP solution and to get rid of all the analog phones but our users are now complaining that their phone call quality is horrible. We contact the open source VoIP solution provider
and we find out that they thought it would be a good idea to use a reliable transport protocol like
TCP since well, we want phone calls to be reliable right?
Wrong thinking! TCP does error correction which means that data that didn’t make it to our
computer will be retransmitted. How weird will our phone call sound if we are talking to
someone and we hear something that they said a few seconds ago? It’s real-time so we don’t want
retransmission. It’s better to send VoIP packets and lose a few than retransmitting them
afterwards, our VoIP codec can also fix packet loss up to a certain degree. In this example we’ll
want to use a best effort or unreliable protocol which is UDP.