Understanding TCP and UDP: The Backbone of Internet Communication

cakehalocakehalo
6 min read

If you’re into IT, you’ve likely heard the terms TCP and UDP. But what are those exactly? What are they used for? Why do we need them?

In this article, I will answer these questions and explain why both protocols are crucial for modern networking.

$ History of TCP and UDP

In the 70s, there was a growing demand for reliable connections between network devices. Under the ARPANET Project, Vint Cerf and Bob Kahn described a networking protocol that provides stable connection, lossless communication, and error-checking. Their idea ultimately turned into reality with the name Transmission Control Protocol (TCP).

As an alternative to TCP, User Datagram Protocol was introduced in the 80s. Unlike TCP, UDP is fast and simple. It is a connectionless protocol.

$ So… what is TCP and UDP??

TCP and UDP are communication protocols. They define how network devices communicate with each other and how data is being sent. They both reside in the IP suite (TCP/IP) and also in OSI Layer 4.

I recommend checking out these links if you are not sure about what these things mean. These articles are not in-depth, so if you’d like to know more I advise you to do your own research and check out multiple sources too!

Khan Academy - The Internet Protocol Suite (TCP/IP)

Imperva - What is the OSI Model

$ And what are the differences?

The key difference lies in the way they transfer data, but there are other differences, of course. Here is a brief comparison:

TCP

  • Uses a “Three-Way Handshake” (see the picture down below)

  • Establishes a connection before sending data

  • More reliable, since delivery is guaranteed

  • Sends packets in order

  • Can ask for certain packets if it’s not delivered

  • Checks for errors in-depth

  • Doesn’t allow broadcasting

Source

As you can see on the diagram, the client (laptop) waits for a reply form the server. This provides a reliable connection between the two hosts. Before diving into any details, let’s check out UDP.

UDP

  • It’s a connectionless protocol

  • Sends data without notifying the server

  • Doesn’t expect replies

  • Can’t restore lost packets

  • Faster than TCP

  • Less reliable

  • Can send data in response to a request

What Is User Datagram Protocol?

Source

At first, UDP might seem a little inefficient and useless, but believe me, there are lots of cases when we must use UDP. We will check out the use cases a bit later in this article, but before that, let’s talk about TCP in a bit more detail.

$ How does TCP work?

As mentioned before, TCP is a very reliable protocol. This is due to the fact it uses a Three-Way Handshake. This works by sending a “notification” to the server so that it knows that it should be ready to receive data. This happens as the following:

  1. The client sends a SYN packet: The client sends a packet to the server that contains the SYN flag (synchronization) and an Initial Sequence Number (ISN) that helps tracking the packets and reassembling them correctly.

  2. The server replies with SYN-ACK: This packet tells the client that the server has successfully received the message and is ready for the actual data. The server sends back an ISN too that helps figuring out what the first sequence number should be for response packets.

  3. The client replies with ACK: This is the final packet that establishes the connection. The client ACK-nowledges the ISN of the server then proceeds to send the actual data.

There are several analogies that can help understanding how TCP works. Here is my favorite one.

Another thing before we move on to UDP: The ISN. I’m not sure if it’s necessary to learn about it in-depth, but it’s worth mentioning for sure.

Both the client and the server chooses an ISN. These will mark the order of the packets sent. For example, the client chooses the ISN of 1000 while the server chooses 2000. (Note that the numbers are chosen randomly for security reasons). This means that the next packet the client sends will be marked with 1001. The reply will be 2001. This goes so on until the connection ends.

Since TCP connections are usually very short, the ISNs will not overlap.

The ISN is the cornerstone of TCP, because it is used for error handling, rearrangement and recovering lost packets.

$ How does UDP work?

UDP works by receiving a request and then sending data without any reassurance. This is infinitely simpler than TCP given that there are no connection to be established. TCP is just sending packets without concern for delivery.

The UDP header doesn’t contain sequence numbers like TCP. It only consists of:

  • Source port: Port of the service sending the data

  • Destination port: Port of the service receiving the data

  • Length: Defines the byte length of the UDP package (including the header)

  • Checksum: Optional field that the receiver can use to check the integrity of the received data

Since TCP and UDP operates in Layer 4 (Transport), they have ports in the header and not IP or MAC addresses. IP addresses are being handled in Layer 3 while MAC addresses are handled in Layer 2.

$ What are they used for?

Before I answer this, please take your time to think about how TCP and UDP can be utilized. What services/software would use TCP? or UDP? Which is better for what?

Let’s look at TCP first. Since TCP is reliable, but slow, it is often used for not real time services. For example, e-mail services, messaging, web browsing. These don’t require very high speed, but rather reliability. It’s important to get all of our messages, not just some of them.

On the other hand, UDP is used for it’s speed. It is mainly used for real-time services, such as video chats, gaming, and live streaming. Unlike TCP, we won’t necessarily receive all of the packets. If we did, we would sacrifice the speed, which is not a good thing for gaming or video chats.

$ Key Takeaways

  • Both TCP and UDP are transport protocols

  • They are used in OSI Layer 4

  • TCP is a reliable, but slower

  • UDP is a fast, but unreliable

  • TCP is used for web browsing, messaging, etc.

  • UDP is used for real-time communication, like video chats and gaming

Thank you for reading my post! I hope this article helped you understand the concept of TCP and UDP. I highly encourage you to visit more sources in order to gain better understanding of this topic. Also, feel free to reach out with your suggestions in the comment section.


https://www.avast.com/c-tcp-vs-udp-difference

https://www.baeldung.com/cs/tcp-protocol-syn-ack

https://www.cloudns.net/blog/udp-user-datagram-protocol-explained-in-details/#UDP_header

0
Subscribe to my newsletter

Read articles from cakehalo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

cakehalo
cakehalo

I’m cakehalo, a Hungarian high school student (for now) with a massive interest in cybersecurity. I first started learning about this and that in middle school, and there’s been no stopping since! With that being said, I’m nowhere near a professional, and I’m always open to feedback and comments. :) https://hyperpwn.hashnode.dev/introduction