TCP vs UDP

TCP vs UDP

In this article, I will go over what TCP and UDP are, the differences between them, use-cases for each, how they work and the three-way handshake.

To understand TCP and UDP, we need to first understand what the Internet Protocol Suite is.

In Short, the Internet Protocol Suite is a collection of different protocols that the internet uses so devices can talk to one another.

TCP and UDP are two major protocols that the internet uses. Some other protocols within the Internet Protocol Suite that you may have heard of include: FTP (File Transfer Protocol), SMTP (Simple Mail Transport Protocol), and HTTP (Hyper Text Transfer Protocol).

Now, lets learn about TCP and UDP!


TCP

  • TCP stands for Transmission Control Protocol
  • It is the most common protocol used on the internet to
    transmit data
  • It is extremely reliable and guarantees that data will be transferred.
  • Has error-recovery features

When Is TCP Used?

  • TCP is used in situations where it is necessary that all data being sent is received by another device and it is guaranteed to do so.

How does TCP Work?

  • TCP is a connection-based protocol. This means that a connection is established between two devices before transferring data and maintains it throughout the transfer process.

  • To establish a connection between two devices, a three way handshake is initiated between the client and server.

TCP Three Way Handshake

For example, to read this article, your device, which could be a smartphone, tablet, or laptop will send a message to the server of this site.

This message is called a Synchronized Sequence Number (SYN).

Then, the message will get to the server and the server will acknowledge that the message was sent and received. It will send back an acknowledgement back to the client machine called SYN-ACK.

Then, the client machine will receive that SYN-ACK message from the server and send back a ACK (acknowledgement) message, resulting in a connection being established.

Once this connection is established, the protocol guarantees all data will be transmitted.

How Does TCP Do Error Recovery?

  • TCP has a mechanism called Windowing to control the flow of data.
  • A Window is the amount of data that can be sent before an ACK message is received.
  • The ACK message will have a forward looking acknowledgment number with the next expected data segment.
  • If this is wrong, the client will sent back the lost/dropped data segment back to the server before moving forward.
  • This will ensure no data is forgotten or lost.

UDP

  • UDP stands for User Datagram Protocol
  • Less reliable than TCP but lighter and faster.
  • Has way less features than TCP.
  • Does not care if data was sent successfully or not
  • Known as "fire and forget" protocol

When is UDP Used?

  • UDP is used for situations when data loss is acceptable and performance is highly criticle.
  • Great examples include: Online Gaming, Live Video/Audio, VoiP

How does UDP Work?

  • UDP is a connection-less protocol. This means that no connection is established beforehand, unlike TCP with the three-way handshake.
  • UDP will just send the data segments. It doesn't care if the server actually receives it and it doesn't do any error recovery either. This is why it is called the "fire