Lesson 4 of 14 · 10 min · How networks work

TCP, UDP and ports

Covers: Ubuntu 24.04 LTS, tcpdump 4.99, iproute2 6.1. Updated 13 Nov 2026. Part of the DevOps Engineer roadmap.

What you will learn

  • What ports and sockets are, and the port number ranges
  • How TCP sets up and closes a connection, and how it keeps delivery reliable
  • How UDP differs and when it is the right choice
  • The TCP states you see in ss, and what TIME_WAIT means

In simple words

A machine runs many services at once, so each one listens at its own numbered door, called a port. TCP is like a phone call: both sides connect first, and everything said is confirmed. UDP is like shouting across a field: fast, but with no proof it was heard. This lesson covers both.

Words to know

  • Port: a numbered door on a machine. Web servers use 80 and 443.
  • TCP: a reliable way to send data: it connects first, then re-sends anything that gets lost.
  • UDP: a fast way to send data with no connection and no delivery check.
  • Handshake: the three short messages that open a TCP connection.

Ports and sockets

An IP address gets a packet to a machine. A port number (0 to 65535) gets it to the right program on that machine. A connection is identified by four values, the socket pair: source IP, source port, destination IP, destination port.

  • Well-known ports (0-1023) belong to standard services and need root to bind: 22 SSH, 25 SMTP, 53 DNS, 80 HTTP, 110 POP3, 143 IMAP, 443 HTTPS, 587 mail submission, 993 IMAPS.
  • Registered ports (1024-49151) are used by applications by convention: 3306 MySQL, 5432 PostgreSQL, 6379 Redis, 8080 alternative HTTP, 9090 Prometheus, 27017 MongoDB.
  • Ephemeral ports (about 32768-60999 on Linux; see /proc/sys/net/ipv4/ip_local_port_range) are picked by the client for each outgoing connection. Your browser's connection to port 443 might leave from port 52314.

A server binds (listens) on a port; clients connect to it. ss -tlnp shows listeners.

TCP: reliable, ordered streams

TCP (Transmission Control Protocol) gives programs a reliable, ordered stream of bytes between two sockets, built on unreliable IP. It adds:

  • Connection setup and teardown.
  • Sequence numbers and acknowledgements: each byte is numbered; the receiver acknowledges what arrived; missing data is retransmitted.
  • Ordering: data is delivered to the application in the order sent.
  • Flow control: the receiver advertises a window (how much it can accept) so a fast sender does not overwhelm a slow receiver.
  • Congestion control: the sender slows down when the network shows loss, so the network is shared fairly.

The price is latency: setup round trips, waiting for acknowledgements, and a stalled stream when a packet is lost (head-of-line blocking).

The three-way handshake

Client                         Server
  |  --- SYN (seq=x) ------->    |   "I want to talk; my numbers start at x"
  |  <-- SYN-ACK (seq=y, ack=x+1)|   "OK; mine start at y"
  |  --- ACK (ack=y+1) ----->    |   "Got it"          -> connection established

We captured a real one on the loopback interface while curl fetched a page from a local server (tcpdump -n -i lo 'tcp port 8080'):

IP 127.0.0.1.45354 > 127.0.0.1.8080: Flags [S],  seq 1775980485, ...
IP 127.0.0.1.8080 > 127.0.0.1.45354: Flags [S.], seq 1456745285, ack 1775980486, ...
IP 127.0.0.1.45354 > 127.0.0.1.8080: Flags [.],  ack 1, ...
IP 127.0.0.1.45354 > 127.0.0.1.8080: Flags [P.], seq 1:78, ack 1, ...      (the HTTP request, 77 bytes)
IP 127.0.0.1.8080 > 127.0.0.1.45354: Flags [.],  ack 78, ...
IP 127.0.0.1.8080 > 127.0.0.1.45354: Flags [P.], seq 1:157, ack 78, ...    (the response, 156 bytes)

Read the flags: S is SYN, S. is SYN-ACK (the dot means ACK), . a plain ACK, P. is PUSH+ACK (data), F FIN, R RST. The client port 45354 is the ephemeral port. Notice that the first packet's seq 1775980485 is answered with ack 1775980486: the acknowledgement number is the next byte expected.

Closing and the states

A connection ends with FIN packets from each side (or a RST, an abrupt reset, when something is wrong, for example connecting to a closed port). ss -tan shows the state of each TCP socket:

State Meaning
LISTEN a server waiting for connections
SYN-SENT the client sent SYN, waiting for the answer. Many stuck here means a firewall drops traffic or the server is unreachable
SYN-RECV the server got SYN, sent SYN-ACK. Many here can indicate a SYN flood
ESTABLISHED the normal state of a working connection
FIN-WAIT, CLOSE-WAIT closing. Many CLOSE-WAIT means the application is not closing sockets it was told the other side closed: a bug
TIME-WAIT the side that closed first waits (about 60 s) to be sure late packets are gone. Thousands are normal on busy clients and rarely a problem by themselves

Count them: ss -tan | awk 'NR>1{print $1}' | sort | uniq -c.

Connecting to a port where nothing listens gets an immediate RST: "connection refused". If a firewall silently drops the SYN, the client keeps retrying and ends with "timed out". This difference is the basis of the diagnosis in the Linux troubleshooting lesson.

UDP: fast and simple

UDP (User Datagram Protocol) sends independent datagrams: no connection, no acknowledgement, no retransmission, no ordering. A datagram may be lost, duplicated or arrive out of order, and the application must cope. In exchange it has no setup delay and very little overhead.

TCP UDP
Connection yes (handshake) no
Reliability and ordering yes no
Overhead and latency higher lower
Message boundaries none (a byte stream) kept (each datagram)
Typical use web, SSH, databases, email, file transfer DNS queries, video calls, games, streaming, VoIP, DHCP, NTP, QUIC/HTTP3

Choose UDP when fresh data matters more than perfect data (a late video frame is useless) or when messages are tiny and one round trip is enough (DNS). QUIC, which HTTP/3 uses, builds reliable, encrypted streams on top of UDP to avoid TCP's head-of-line blocking and to connect faster.

Check UDP listeners with ss -ulnp. A UDP port that nobody listens on usually answers with an ICMP "port unreachable"; with firewalls often there is no answer at all, which makes UDP harder to test than TCP.

Practical points for DevOps

  • A "port" in a firewall rule or security group must name the protocol: TCP 443 and UDP 443 are different (HTTP/3 uses UDP 443).
  • Bind address matters (127.0.0.1 or 0.0.0.0), as seen in the Linux course.
  • Ephemeral port exhaustion: a client making very many short connections to one server can run out of source ports (each socket pair must be unique), showing as sporadic connection failures. Use keep-alive or connection pools.
  • Timeouts and retries: set client timeouts. A TCP connection to a dead host may wait minutes by default.
  • Keep-alive reuses a connection for many requests, saving handshakes.

Common mistakes

  • Saying "TCP is faster than UDP" or the reverse without context. UDP has less overhead; TCP does more work to be reliable.
  • Opening a TCP rule but needing UDP (DNS over UDP, WireGuard, QUIC).
  • Treating TIME-WAIT as a leak.
  • Ignoring CLOSE-WAIT buildup, which points to application bugs.
  • Assuming "port open" in nmap means the service works. It only means the TCP handshake succeeded.

Try it yourself

  1. Run python3 -m http.server 8080 --bind 127.0.0.1, capture with sudo tcpdump -n -i lo 'tcp port 8080', fetch with curl, and identify SYN, SYN-ACK, ACK and the data packets.
  2. Connect to a closed port and to a port blocked by a firewall rule that drops packets; compare the errors and timing.
  3. Use ss -tan and ss -s to count connection states on your machine.
  4. Look up which protocol and port DNS, NTP, DHCP and SSH use.

Check yourself

What happens in the TCP three-way handshake?

The client sends SYN, the server replies SYN-ACK, the client replies ACK. After this both sides know each other's initial sequence numbers and the connection is established.

Give two reasons to choose UDP over TCP.

Low latency without connection setup, and tolerance for loss where fresh data matters more than complete data (real-time audio/video, games), or very small request/response exchanges such as DNS.

What does "connection refused" tell you compared with "timed out"?

Refused: the host replied with RST, so nothing is listening on that port (the network path works). Timed out: no reply, usually a firewall drop or a routing problem.

What is TIME-WAIT, and is a large number of them a problem?

The state of the side that closed a connection first, waiting about 60 s so stray packets die out. Many are normal on busy clients and only a problem if they cause port exhaustion.

Interview questions

How does TCP ensure reliable delivery?

Sequence numbers, acknowledgements and retransmission of unacknowledged data, plus ordering at the receiver, flow control with the window, and congestion control.

Why does DNS normally use UDP?

Queries and answers are small and a single round trip, so UDP avoids handshake cost. It falls back to TCP when the answer is too large or for zone transfers.

What is a socket?

An endpoint of communication identified by an IP address, a port and a protocol. A TCP connection is uniquely identified by the pair of sockets at its ends.

Summary

  • Ports identify programs; a TCP connection is the pair (src IP, src port, dst IP, dst port).
  • TCP gives reliable ordered streams using the three-way handshake, sequence numbers, ACKs, windows and congestion control; UDP gives bare datagrams with low overhead.
  • Know the ss states: SYN-SENT stuck means blocked, CLOSE-WAIT buildup means an application bug, TIME-WAIT is normal.
  • Firewall rules name protocol and port; UDP and TCP are separate.

Going deeper

See how much of this you can explain

A free mock interview asks you new questions on Networking for DevOps for DevOps Engineer and scores every answer, with what to fix.