Internet Protocol: TCP vs UDP

We will go through an introduction, you may jump to SECTION 2: TCP vs UDP.

SECTION 1: Introduction

The Internet Runs On Standards. To allow data transmission, every user needs to agree on a standard format for the bytes.

What is the Internet

Internet is a worldwide network. You can think of it as a network of networks. Computers are connected on the internet through a sequence of routers, the connection flows through the hierarchy in networks.

For example, your home router will connect to a bigger router, then the next router, “hop hop hop” closer to its ultimate destination.

Fun fact: Each router only knows enough to figure the next hop, but not the whole route. Routers are a distributed, collaborative system instead of a centralized, top-down system.

Transmission of Data

When the data is transmitted, it is divided into packets. The network transmits one packet at a time. The receiving computer then collects and reassembles the data bytes to recover the original file or webpage.

With Ethernet wire, the sending computer sends a series of voltage and the receiving computer reads it off the wire based on a voltage scheme.

On Wifi, without wire, it works like a radio channel, it broadcasts the data to all computers in the reachable field. Everyone sees the data but only the right recipient computer accepts it, others should ignore the packet.

Internet Protocol (IP) address

Every computer on the internet has an IP address based on its location. It consists of 4 bytes, which each byte contains a number, for example, 171.64.64.166 (IP version 4 / IPv4).

Domain systems can look up an IP address of a domain name. All packet sending uses an IP address.

The packets define data format, checksum scheme, and also the addresses which it has both ‘from:’ and ‘to:’ fields.

10.x.x.x and 192.168.x.x addresses are special “local” IP addresses. They are translated to a real IP address as a packet makes its way. (DHCP standard)

Experiment: Bring up the networking control panel of your computer. It should show what IP address you are currently using and the IP address of your router.

IPv6 vs IPv4

IPv6 is the new version of Internet Protocol, which is way better than IPv4 in terms of complexity and efficiency.

Instead of a numeric address separated by a dot (.), IPv6 is an alphanumeric address whose binary bits are separated by a colon (:). For example, 2001:0db8:0000:0000:0000:ff00:0042:7879.

IPv4 consists of 4 bytes (32 bit) while IPv6 consists of 32 bytes (128 bit).

All 4.29 billion (2³²) IPv4 addresses have now been assigned, leading to an address shortage issue. Thus, we have IPv6 addresses that are able to accommodate a huge number of addresses (2¹²⁸) that are 1028 times larger than IPv4 addresses. IPv6 would eventually replace IPv4 and avoid the shortage issue in the near future.

Also, there are other enhancements in IPv6 like better multicast routing, simpler header format, auto-configuration, no more NAT (Network Address Translation), and more. It allows easier administration (no more DHCP) that each user has a permanent address.

Communication Protocol

There are two common protocols to send and receive data, TCP and UDP.

The standards are “open standards” which are not under the control of any company. Open standards turned out to be the key enabler to the internet as we know it today.

Let’s keep reading to learn:

  1. What are they?
  2. What is the difference between them?

SECTION 2: TCP VS UDP

The internet protocol suite is a collection of different methods to send information across the internet. Both TCP and UDP are major protocols within the internet protocol suite.

Internet protocol suite (source: https://www.sciencedirect.com/topics/computer-science/internet-protocol-suite)

TCP — Transmission Control Protocol

TCP is used in situations where the data should be received by the receiver correctly.

For example, if the webpage data is not received correctly, the page will break and you won’t be able to see it.

TCP is connection-oriented which establishes a connection between two devices before transferring data.

When the connection detects negative acknowledgment or false checksum, the sender will resend that packet to the receiver. However, the checking and resending data will add latency.

Properties:

  • Reliable (Most commonly used)
  • Data can be transmitted in two directions
  • Eg. send an email, view a webpage, or download a file
  • It includes these actions:
  1. Acknowledgments (Three-way handshake — SYN, ACK, SYN-ACK)
  2. Sequencing (One field the states the position of data in the header)
  3. Checksum (An algorithm that checks if data at both ends are the same)

Their individual purpose is:

- Acknowledgements check whether or not the connection is totally reliable.

- Sequencing helps the receiver to arrange the packets in the right order.

- Checksum allows the receiver to notice that a packet did not come through right.

Three-way handshake (source: https://www.techopedia.com/definition/10339/three-way-handshake)
  • TCP has a 20–60 bytes variable length header.
Each packet header with TCP protocol (source: https://www.youtube.com/watch?v=cA9ZJdqzOoU)

UDP — User Datagram Protocol

UDP is used for situations where some data loss is acceptable and real-time performance is more important.

For example, in a voice call, you will have troubles if you and your partner have each other replies delayed, you both might speak at the same time and only knew it seconds later.

If some packets are lost along the way in a voice call, it will just appear as a bit of lag or clipped video/audio, thus not affecting the effectiveness of communication.

UDP is connectionless which does not establish a connection like TCP. It simply sends out data to the device that requests it and will not resend it.

Properties:

  • Unreliable
  • Data transmitted in one direction (Only send data)
  • No error handling or sequencing (Only have the basic checksum)
  • Low latency / Real-time connection
  • Eg. Voice call, video call, and gaming
  • UDP only has an 8 bytes fixed-length header.
Each packet header with UDP protocol (source: https://www.youtube.com/watch?v=cA9ZJdqzOoU)

Quick Recap

Thank you for reading. Hope you have a good read.

Reference

  1. https://www.youtube.com/watch?v=cA9ZJdqzOoU
  2. https://www.freecodecamp.org/news/tcp-vs-udp/
  3. https://www.geeksforgeeks.org/differences-between-tcp-and-udp/
  4. https://web.stanford.edu/class/cs101/network-3-internet.html
  5. https://youtu.be/7_LPdttKXPc

About Author

This article is written by Han Sheng, Technical Lead in Arkmind, Malaysia. He has a passion for Software Design/Architecture related stuff, Computer Vision and also Edge Devices. He made several AI-based Web/Mobile Applications to help clients solving real-world problems. Feel free to read about him via his Github profile.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Draden Liang Han Sheng

Full Stack AI Application Development | Computer Vision | Deep Learning | Edge Devices