What is TCP and UDP? A Simple Explanation

The internet depends on two protocols for the exchange of information between computers. These communication protocols define the rules that determine how data transfer occurs between any two entities involved, whether one-to-one or one-to-many. Transmission Control Protocol (TCP) and User Datagram Protocol (UDP) are two protocols you can use to move data across networks. They both work at the transport layer of the OSI model. 

Similar yet different, these two protocols do their jobs efficiently. While one is connection-oriented and requires establishing a connection before data transfer can start, the other is connectionless and sends data without informing the destination device first. There’s more to what these protocols do, as you will find out below. This article focuses on TCP and UDP — how they work, their advantages, disadvantages, and differences. 

What is TCP?

Transmission Control Protocol is a communication protocol that deals mainly with the transfer of data packets on the internet. It operates at the transport layer (layer 4) of the Open Systems Interconnection (OSI) model. This protocol aids in transferring data packets between a client device/application and a server (along with UDP). 

TCP is connection-oriented, which means that the client and server must establish a connection before sending data in any direction. In addition, it is a reliable protocol as it checks for errors and ensures data packets arrive at their destination in the order that the sending device or application intended. 

How Does TCP Work?

When using TCP, a client and a server establish a connection using what is known as the three-way handshake before the bidirectional transfer of data packets begins. Before the three-way handshake begins, the server must be listening at a port to accept the connection. The handshake begins with the exchange of three messages (or flags) known as SYN, SYN/ACK, and ACK between the client and the server. 

Let’s look at a step-by-step process of the exchange below. 

1. The client sends a SYN data packet to the server. The SYN packet contains a random sequence number (X) that the client assigns. The purpose of this packet is to see if the server is available for a new connection. 

2. The server receives the SYN packet and acknowledges it by sending a SYN/ACK packet. The server sets the acknowledgment number to one plus the sequence number (1+X) the client assigned to the initial SYN packet and sets another random sequence number (Y). So the server sends two sequence numbers along with its SYN/ACK packet to the client.

3. The client receives the SYN/ACK packet from the server and acknowledges it by sending an ACK packet. The client sets the acknowledgment number to one plus the sequence number (1+Y) it received from the server. 

Once the three-way handshake process is complete, the client and the server can send and receive data from each other. 

Advantages and Disadvantages of TCP

Like most protocols, TCP is not flawless; it has its advantages and disadvantages. Let’s look at the pros and cons of using TCP below. 

Advantages

1. Reliable

TCP is reliable because it ensures that data packets arrive at their destination in the correct order. When TCP transmits a data segment, it runs a timer and waits for an acknowledgment from the destination. It will resend the segment if it does not receive any acknowledgment. In addition, it uses sequencing to know when data packets arrive out of order.

2. Error Detection and Recovery

TCP uses an end-to-end checksum to check for any changes in the packets as they arrive at their destination. It discards packets with errors and waits for the sender to resend them. In addition, it discards data duplicates as well. 

3. Congestion Control

TCP uses buffering and windowing to handle congestion. Congestion can happen when the receiving end of the TCP connection receives too many data packets than it can take at a time. TCP uses buffers to handle congestion, but when buffers become full, it uses windowing to reduce the amount of data the sender can send at a time. 

Disadvantages

1. Slow Speed

Compared to UDP, TCP is slow. Firstly, the necessary three-way handshake takes time. Then, there is the speed reduction that flow control, congestion control, and error checking cause during data transfer. In such cases, TCP sacrifices speed for reliability. 

2. Vulnerable to DoS Attacks

TCP’s three-way handshake makes it vulnerable to DoS or DDoS attacks. A SYN flood is one such attack where a malicious entity makes a server unavailable by flooding it with SYN packets. This attack aims to make the server overwhelmed, preventing it from responding to genuine requests. 

What is UDP?

User Datagram Protocol (UDP) is a communication protocol like TCP that operates at the transport layer of the OSI model. Unlike TCP, it is a connectionless protocol, which means that a client does not need to establish a connection with the server before it transmits data packets. As a result, there is no three-way handshake, flow control, and sequencing. 

UDP does not care if the datagrams sent arrive at their destination or if they arrive in the order in which the client sent them. This makes it less reliable than TCP but faster since it does not have to wait for acknowledgments or retransmit data. However, UDP cares about data integrity, and it uses a checksum to confirm a third party has not tampered with datagrams. 

How Does UDP Work?

With UDP, the movement of datagrams from their origin to destination is an easy process. After adding its header to the IP packets it receives from the network layer, it sends datagrams directly to the destination server without needing to establish any connection first. It is the simple nature of this process that makes UDP a connectionless protocol. 

In addition, UDP is ideal for time-sensitive communications where the loss of data in transit is acceptable. For example, voice and video communications use UDP. 

Advantages and Disadvantages of UDP

UDP’s lack of flow control and data retransmission is both an advantage and a disadvantage. Below, we look at the pros and cons of using UDP. 

Advantages

1. Fast Data Transmission

Since UDP does not need to acknowledge datagrams before the destination computer can receive more and does not retransmit data, it is faster than TCP. In addition, the absence of a three-way handshake results in less overhead and quicker data transmissions. 

2. Error Detection

Like TCP, UDP uses an end-to-end checksum to detect if datagrams have any errors. By doing so, it maintains data integrity and drops datagrams with errors. Since there is no retransmission mechanism, UDP does not recover or resend datagrams it drops. 

Disadvantages

1. Unreliable

Features that make a connection reliable are absent from UDP connections. Unlike TCP, UDP does not guarantee datagrams sent will reach their destination. As a result, datagrams don’t have a timer or need to be acknowledged by the receiving device. In addition, there is no assurance that datagrams will arrive at their destination in the proper order. 

2. No Congestion Control

UDP has no mechanism to control congestion. Since it doesn’t acknowledge datagrams or use windowing to control data transmission, there is no way to prevent congestion. As a result, large bursts of data can result in congestions. 

Differences Between TCP and UDP

Below, we will look at the differences between connection-oriented TCP and connectionless UDP.

1. Reliability

TCP is a more reliable protocol compared to UDP. TCP will confirm a data segment reaches its destination before sending another one. Furthermore, it will ensure segments arrive in the same sequence they were sent and retransmit data that doesn’t make it to the destination. In addition, TCP uses flow control to ensure that the client does not send packets faster than the server can process them. On the contrary, UDP does not do any of the above and cares more about transferring data as fast as it can.  

2. Speed

The features of TCP that make it reliable make it slower than TCP. The three-way handshake, flow control, congestion control, and retransmission mechanism are not part of UDP. As a result, with less overhead, UDP is faster than TCP. 

3. Usage

TCP’s connection-oriented data transfer makes it suitable for applications that require reliability and cannot accept losing data. For example, it supports protocols such as HTTP, SSH, FTP, SMTP, and IMAP. On the other hand, UDP’s speed makes it suitable for time-sensitive applications or protocols that can accept data loss, such as live video or audio chats, Voice over IP, DNS requests, and online gaming. 

Conclusion

TCP and UDP are the bedrock of communication on the internet. Without these two protocols, it will be challenging for devices connected to the internet to communicate with each other. TCP is the go-to protocol if you need reliability and accuracy. On the other hand, UDP is the protocol you need if you’re less concerned about losing data packets and more concerned about them reaching their destination fast.