Course contents

Unit 7: TCP, UDP and PortsLesson 7.1.11 (11 of 23 in this unit)60 of 104 in the Network Fundamentals course

TCP vs. UDP compared

Should an application choose TCP or UDP? Start with what the application needs to recover, preserve or deliver before a deadline—not a slogan that one protocol is faster.

Beginner · 5 min read

Covers Reliability vs. speed and Choosing a protocol.

Technical definition

TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are the two main transport layer protocols. TCP is connection-oriented and delivers a reliable, ordered byte stream with flow control; UDP is connectionless and sends independent datagrams with an 8-byte header and no delivery guarantee.

Firewall rules that must say TCP or UDP

Your team is writing firewall rules for a new server. It runs a website, a DNS service and a voice system. For each one, the rule must say TCP or UDP, not just a port number. If you pick the wrong protocol, the service breaks. So you need to know which protocol each application uses, and why.

The two protocols side by side

Ask what should happen after a loss. A file transfer needs the missing bytes recovered before it can reconstruct the file. A real-time voice application may choose to conceal or discard an old frame whose playback deadline has passed. A program using UDP can still implement retries, ordering and congestion control; those features simply are not supplied by UDP itself.

Compare the complete application protocol, not just the TCP or UDP header. QUIC builds reliable streams over UDP, while a failed TCP connection can still leave a download incomplete. Neither TCP nor bare UDP encrypts the application data by itself; HTTPS uses additional security protocols. Your design choice trades implementation work, overhead and delivery behaviour.

Diagram
The two protocols side by side
The two protocols side by side
TCPUDP
ConnectionConnection-oriented: three-way handshake firstConnectionless: sends straight away
DeliveryReliable: lost data is resentBest effort: lost data stays lost
OrderData is passed to the application in orderNo ordering
Flow and congestion controlYes (windowing)No
Header size20–60 bytes8 bytes
Data unitSegment (part of a byte stream)Datagram (one separate message)
Broadcast and multicastNot possiblePossible
Typical usesWeb, email, file transfer, SSHVoice, video, DNS, DHCP, NTP, games
Read each row across: the left box names the item, the others describe it.

How soon the first data arrives

Diagram
How soon does the first data arrive?
How soon does the first data arrive?
ClientRouterTCP serverweb, TCP 443UDP serverDNS, UDP 53

Device key: Client (laptop)

Lines show the example’s network connections. Thick links: uplinks/WAN, as labelled. The coloured path and envelope follow the current step; its text identifies the sender, receiver and traffic type.

  1. 1. TCP, trip 1: the client asks to connect. No data yet.
  2. 2. TCP, trip 2: the server agrees. There is still no data.
  3. 3. TCP, trip 3: only now can the request be sent.
  4. 4. UDP, trip 1: the very first datagram already carries the question.
  5. 5. UDP, trip 2: the answer comes back, after just one round trip.

Why a video call prefers loss to delay

Neither protocol is “better”; each is a trade-off. TCP spends time and bandwidth to make sure every byte arrives, in order. UDP adds almost no overhead and leaves loss and ordering to the application.

For example, a voice application can conceal or discard a late frame to meet a playback deadline. UDP lets it make that choice; it does not guarantee that loss is harmless. An ordered TCP stream can delay delivery of later bytes while earlier bytes are recovered. The application's deadline and recovery design determine the visible effect.

Five questions that pick the protocol

Ask these questions about the application:

QuestionIf yes
Must every byte arrive, exactly as sent? (files, web pages, logins)TCP
Is late data useless? (live voice, video, games)UDP
Is it one short question and answer? (DNS, NTP)UDP
Must it reach many receivers at once, or a host with no address? (multicast, DHCP)UDP
Is it a long transfer that should share the link fairly?TCP

Some services use both. Classic DNS supports UDP and TCP queries; TCP also supports zone transfers and fallback after truncation. HTTPS can use TCP 443, while HTTP/3 runs over QUIC on UDP 443.

That last point catches people out when writing firewall rules: a rule that permits only TCP 443 may still allow sites to load if the client and server support a TCP-based fallback. That fallback is not guaranteed for every application, and a working page does not prove that UDP 443 is permitted.

Counting TCP and UDP hits on an ACL

On a Cisco router, each access control list (ACL) rule names the protocol, so the match counters show how much traffic of each kind arrives. The output below is based on Cisco documentation, not captured from a lab device.

Example output · based on Cisco documentation; exact format varies by platform and software version
R1#show access-lists EDGE-IN
Extended IP access list EDGE-IN
    10 permit tcp any host 203.0.113.10 eq 443 (15230 matches)
    20 permit udp any host 203.0.113.10 eq 443 (40112 matches)
    30 permit udp any host 203.0.113.53 eq domain (9921 matches)
    40 permit tcp any host 203.0.113.53 eq domain (37 matches)
What to look for: the matches count on each line. The web server receives both TCP 443 (HTTP/1.1 and HTTP/2) and UDP 443 (HTTP/3). The DNS server (eq domain means port 53) receives mostly UDP, plus a few TCP queries for large answers or zone transfers. Leaving out line 40 would break those large lookups.

Test yourself on choosing TCP or UDP

Predict · scenario 1

A company backs up its database to a remote server every night. Which protocol fits best?

Predict · scenario 2

A video system must send the same stream to 200 screens at once using a single multicast address. Why does it use UDP rather than TCP?

Predict · scenario 3

A firewall allows UDP 53 to a DNS server, but not TCP 53. What might fail?

Technical references

RFC 9293: base TCP header, connection state, sequence space and delivery; RFC 768: UDP fields; RFC 8085: application responsibilities over UDP; RFC 9000: QUIC over UDP; RFC 7766: DNS over TCP.