1. Home
  2. Explore
  3. Computer Science
  4. TCP vs UDP

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

TCP vs UDP

TCP and UDP: Layer 4 Protocols

These notes cover the Transport Layer (Layer 4) of the OSI model, focusing on the two main protocols: TCP (Transmission Control Protocol) and UDP (User Datagram Protocol). The goal is to compare their characteristics and understand when each is used.


Layer 4 Overview

The Transport Layer provides transparent transfer of data between end hosts. This means that the hosts send and receive data without being aware of the underlying network details—the lower layers (3, 2, 1) handle the actual delivery.

Key Functions of Layer 4

  • Layer 4 Addressing – Uses port numbers to identify specific applications or services and to enable session multiplexing (handling multiple simultaneous communications).

  • Service Provision (or Not) – Some Layer 4 protocols (like TCP) offer extra services such as reliable delivery, error recovery, sequencing, and flow control. Others (like UDP) do not.

Port Numbers are 16-bit values (0–65535) assigned by the IANA (Internet Assigned Numbers Authority). They serve two main purposes:

  1. Identify the Application Layer protocol (e.g., HTTP uses port 80).

  2. Track individual communication sessions when combined with source/destination IPs.

The three ranges of port numbers:

  • Well‑known ports: 0–1023 – used by major protocols (HTTP, FTP, etc.), strictly regulated.

  • Registered ports: 1024–49151 – require registration but less strict.

  • Ephemeral (dynamic/private) ports: 49152–65535 – used by hosts for random source port selection.

When a host initiates a connection, it randomly picks an ephemeral source port. The destination port is determined by the application (e.g., 80 for HTTP). In the reply, source/destination ports are swapped, allowing the original host to recognise which session the reply belongs to.


TCP – Transmission Control Protocol

TCP is a connection‑oriented, reliable protocol. Before exchanging actual data, the two hosts establish a connection. TCP provides:

  • Reliable data transfer – every segment must be acknowledged; lost segments are retransmitted.

  • Error recovery – if a segment is corrupted or lost, it is resent.

  • Data sequencing – segments are numbered so the receiver can reorder them if they arrive out of order.

  • Flow control – the receiver can tell the sender to slow down or speed up using a sliding window mechanism.

TCP Header (simplified)

Key fields for the CCNA:

  • Source Port (16 bits) and Destination Port (16 bits).

  • Sequence Number (32 bits) – used for ordering and reliability.

  • Acknowledgment Number (32 bits) – uses forward acknowledgment (the next expected sequence number).

  • Flags – includes SYN (synchronise), ACK (acknowledge), and FIN (finish) to establish and terminate connections.

  • Window Size (16 bits) – controls how much data can be sent before an acknowledgment is required (flow control).

Three‑Way Handshake (Connection Establishment)

  1. SYN – Client sends a TCP segment with SYN flag set and a random initial sequence number.

  2. SYN‑ACK – Server responds with SYN and ACK flags set, its own random sequence number, and an acknowledgment of the client’s sequence number +1.

  3. ACK – Client sends an ACK segment, completing the handshake.

After this, data transfer begins. The three‑way handshake ensures both sides are ready to communicate.

Connection Termination (Four‑Way Handshake)

  1. FIN – One host (e.g., client) sends a segment with FIN flag set.

  2. ACK – The other host acknowledges.

  3. FIN – The other host then sends its own FIN.

  4. ACK – The original host acknowledges, and the connection is closed.

Sequencing and Acknowledgment Example

  • Host A sends a segment with Sequence Number = 10. Host B acknowledges with Acknowledgment Number = 11 (forward acknowledgment: it expects the next byte to be 11).

  • If Host A sends segment 20 and never receives an acknowledgment (e.g., the segment is lost), Host A will retransmit that segment after a timeout. This is TCP retransmission.

Flow Control – Sliding Window

The Window Size field tells the sender how many bytes it can transmit without waiting for an acknowledgment. The sender can increase or decrease this window dynamically. If a segment is dropped, the window size is reduced, then slowly increases again. This prevents the receiver from being overwhelmed.


UDP – User Datagram Protocol

UDP is a connectionless, unreliable protocol. It does not provide any of TCP’s extra services. It is sometimes called a best‑effort protocol.

UDP Characteristics

  • No connection establishment – data is simply sent.

  • No acknowledgments – if a segment is lost, it is not retransmitted.

  • No sequencing – segments may arrive out of order; the receiver has no way to reorder them.

  • No flow control – the sender can transmit at any rate.

UDP Header

The UDP header is only 8 bytes, containing:

  • Source Port (16 bits)

  • Destination Port (16 bits)

  • Length (16 bits) – total segment length

  • Checksum (16 bits) – for error detection (but no error recovery)

Because the header is smaller and there is no handshake or acknowledgment overhead, UDP is faster and adds less latency.

When to Use UDP vs TCP

Use TCP when…

Use UDP when…

Reliable delivery is essential (e.g., file downloads, emails)

Speed and low latency are critical (e.g., VoIP, video streaming, online gaming)

Data must arrive in order

Occasional packet loss is acceptable (e.g., audio/video calls)

Applications can tolerate some delay for reliability

Applications provide their own reliability (e.g., TFTP, Skype)

Some applications use both TCP and UDP in different situations. For example, DNS normally uses UDP but switches to TCP for large responses or zone transfers.


Comparison of TCP and UDP

Shared features (both provide):

  • Layer 4 addressing via port numbers

  • Session multiplexing

TCP‑only features:

  • Connection‑oriented (three‑way handshake)

  • Reliable delivery (acknowledgments, retransmissions)

  • Error recovery

  • Data sequencing

  • Flow control (sliding window)

UDP‑only features:

  • Connectionless (no handshake)

  • No reliability (best‑effort)

  • No sequencing

  • No flow control

  • Smaller header (8 bytes vs. 20 bytes for TCP)


Important Well‑Known Port Numbers

Protocols using TCP

  • FTP (File Transfer Protocol) – ports 20 (data) and 21 (control)

  • SSH (Secure Shell) – port 22

  • Telnet – port 23

  • SMTP (Simple Mail Transfer Protocol) – port 25

  • HTTP (Hypertext Transfer Protocol) – port 80

  • POP3 (Post Office Protocol 3) – port 110

  • HTTPS (HTTP Secure) – port 443

Protocols using UDP

  • DHCP (Dynamic Host Configuration Protocol) – ports 67 (server) and 68 (client)

  • TFTP (Trivial File Transfer Protocol) – port 69

  • SNMP (Simple Network Management Protocol) – ports 161 (queries) and 162 (traps)

  • Syslog – port 514

Protocol using Both TCP and UDP

  • DNS (Domain Name System) – typically uses UDP port 53; uses TCP port 53 for zone transfers or when responses exceed 512 bytes.


Quiz Review (Key Takeaways from Practice Questions)

  1. Well‑known ports range from 0 to 1023. (e.g., port 1010 is in that range; 2001 and 4023 are registered; 65000 is ephemeral.)

  2. Source port numbers are randomly selected from the ephemeral range (49152–65535).

  3. TCP‑only features (not provided by UDP) include error recovery, flow control, and sequencing. (Layer 4 addressing and session multiplexing are common to both.)

  4. TCP‑based application protocols from the list: SMTP, HTTPS, SSH. (SNMP, DHCP, and Syslog use UDP.)

  5. Forward acknowledgment: if a segment with sequence number 27 is received, the acknowledgment field is set to 28 (the next expected sequence number). This is true when the window size is 1.

Boson ExSim Drag‑and‑Drop Answer

Application Protocol

Transport Protocol(s)

DNS

TCP and UDP

DHCP

UDP

FTP

TCP

HTTP

TCP

SMTP

TCP

SNMP

UDP

TFTP

UDP


Key takeaway: TCP sacrifices speed for reliability; UDP sacrifices reliability for speed. The choice depends on the application’s needs. Memorise the port numbers and which protocols use TCP, UDP, or both for the CCNA exam.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent