Chapter 22 of 28

TCP & UDP

When two devices communicate over a network, simply sending data is not enough. The data also needs to be delivered to the correct application, and sometimes we need to make sure that everything arrives correctly and in the right order.

This is where TCP and UDP come in.

TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are two important Transport Layer protocols used in computer networks.

The easiest way to remember the difference is:

TCP → Reliable and connection-oriented

UDP → Lightweight and connectionless

Let's understand both in a simple way.


What Is TCP?

TCP (Transmission Control Protocol) is a transport-layer protocol that provides reliable, ordered, and connection-oriented communication between applications.

Before exchanging application data, TCP establishes a connection between the two endpoints.

Client                    Server

   |──── Connection ───────→|
   |←──── Established ──────|
   |──── Data ─────────────→|
   |←──── Acknowledgment ───|

TCP keeps track of transmitted data and can retransmit data that is lost.


Main Features of TCP

1. Connection-Oriented

TCP establishes a connection before sending application data.

2. Reliable Delivery

TCP uses acknowledgments and retransmissions to help ensure data reaches the destination.

3. Ordered Delivery

If data arrives out of order, TCP can rearrange it before delivering the byte stream to the application.

4. Error Detection

TCP includes mechanisms to detect corrupted segments.

5. Flow Control

TCP uses a receive window so that a sender does not overwhelm the receiver.

6. Congestion Control

TCP also adjusts its sending behavior based on network congestion.


TCP Three-Way Handshake

Before sending application data, TCP normally establishes a connection using a three-way handshake.

The basic process is:

Client                         Server

  |────── SYN ────────────────→|
  |←──── SYN + ACK ────────────|
  |────── ACK ────────────────→|
  |
  |     Connection Established

The three messages are:

  1. SYN – Client requests a connection.

  2. SYN-ACK – Server acknowledges and responds.

  3. ACK – Client acknowledges the response.

After this, application data can be exchanged.


Where Is TCP Used?

TCP is useful when reliable and ordered delivery is important.

Examples include:

  • Web communication using HTTP/HTTPS over TCP

  • Email protocols such as SMTP and IMAP

  • File transfers

  • SSH

For example, when downloading an important file, losing or reordering parts of the data could cause a problem. TCP's reliability mechanisms help ensure the application receives the byte stream correctly.


What Is UDP?

UDP (User Datagram Protocol) is a transport-layer protocol that provides a connectionless and lightweight way to send datagrams.

Unlike TCP, UDP does not establish a connection using a handshake before sending data.

Sender ───── Datagram ─────→ Receiver
Sender ───── Datagram ─────→ Receiver

UDP does not provide TCP-style guarantees that data will arrive, arrive only once, or arrive in the same order it was sent.


Main Features of UDP

1. Connectionless

UDP does not establish a connection before sending each set of data.

2. Low Overhead

UDP has a small, simple header and does not include TCP's reliability mechanisms.

3. No Guaranteed Delivery

A UDP datagram may be lost without UDP itself retransmitting it.

4. No Guaranteed Ordering

Datagrams may arrive out of order.

5. Application-Controlled Reliability

If an application needs reliability while using UDP, it can implement its own mechanisms.


Where Is UDP Used?

UDP is useful when low overhead, low latency, or application-level control is more important than guaranteed delivery.

Examples include:

  • DNS queries

  • DHCP

  • Online gaming

  • Real-time audio/video

  • Voice and video communication

  • QUIC-based applications such as modern HTTP/3

For example, in a live voice call, receiving an old audio packet several seconds late may be less useful than simply moving on to newer audio. In such situations, the application may prefer a lightweight transport.


TCP vs UDP

Feature

TCP

UDP

Full Form

Transmission Control Protocol

User Datagram Protocol

Connection

Connection-oriented

Connectionless

Reliability

Reliable delivery mechanisms

No built-in guaranteed delivery

Ordering

Provides ordered byte-stream delivery

No ordering guarantee

Retransmission

Yes

No built-in retransmission

Flow Control

Yes

No TCP-style flow control

Congestion Control

Yes

No TCP-style congestion control

Overhead

Higher

Lower

Data Type

Byte stream

Datagram

Speed/Latency

More overhead

Lower overhead

Common Uses

Web, SSH, email, file transfer

DNS, gaming, real-time media, QUIC


TCP vs UDP: Simple Example

Imagine sending a package.

TCP is like a tracked delivery service

You send the package, the system keeps track of delivery, and missing parts can be handled through retransmission.

UDP is like quickly throwing individual messages

You send the messages without waiting for delivery confirmation from the transport protocol.

This makes UDP lightweight, but the application has to deal with reliability itself if it needs it.


TCP and UDP Use Port Numbers

Both TCP and UDP use port numbers to identify applications or services.

For example:

IP Address + Port Number
        ↓
192.168.1.10 : 443

The IP address identifies the destination host at the network layer, while the port number helps identify the destination application or service at the transport layer.

This allows a computer to run multiple network applications at the same time.


Can UDP Be Reliable?

Yes, but there is an important distinction.

UDP itself does not provide TCP-like reliability. However, an application or a protocol built on top of UDP can add mechanisms such as:

  • Acknowledgments

  • Retransmissions

  • Sequence numbers

  • Error detection

For example, QUIC uses UDP as its underlying transport and implements reliability, ordering, encryption, congestion control, and other features at the protocol level.

So don't simply remember:

"UDP is unreliable."

A better statement is:

UDP does not provide built-in reliable, ordered delivery like TCP.


Which One Should We Use?

It depends on the application.

Choose TCP when:

  • Data must arrive reliably

  • Order matters

  • Retransmission is important

  • The application needs TCP's built-in flow and congestion control

Choose UDP when:

  • Low overhead is important

  • The application can tolerate some loss

  • The application needs more control over reliability

  • Timeliness is more important than retransmitting every lost datagram

There is no universally "better" protocol. The correct choice depends on what the application needs.

Conclusion

TCP and UDP are transport-layer protocols, but they are designed for different purposes.

TCP provides connection-oriented, reliable, and ordered byte-stream communication with flow and congestion control.

UDP provides a simpler, connectionless datagram service with lower overhead but without TCP's built-in delivery and ordering guarantees.

The easiest way to remember them is:

TCP → Reliable, ordered, connection-oriented

UDP → Lightweight, connectionless, no built-in delivery guarantee