Cyberlium

Networking › Module 4 › Lesson 1

BeginnerModule 4Lesson 1/4

TCP vs UDP Explained

Connection-oriented TCP vs fast UDP—and when each matters

15 min+24 XP3 quiz
Module progress1 of 4

Opening

An IP finds the host; a port finds the conversation

IPv4 or IPv6 delivers a packet to a machine. That machine may be running SSH, a browser, a DNS stub, and a game at once. Transport ports are how those conversations share one address without mixing their bytes. The 16-bit number is not trivia: it is multiplexing. TCP and UDP are the two ways Layer 4 usually does that job. TCP spends a handshake and acknowledgements to look like a reliable pipe. UDP sends datagrams and lets the application cope. Firewalls, ss output, and outages all change meaning depending on which one you are looking at. You will not craft raw SYN packets in this lesson. You will understand the handshake well enough to read a log, then inventory your own listeners with ss -tuln.

1. Ports multiplex: one IP, many doors, ephemeral sources

A socket is the 4-tuple (or 5-tuple with protocol): local IP, local port, remote IP, remote port, plus TCP vs UDP. Well-known destinations (22, 53, 80, 443) are how clients find a service. The client almost always picks an ephemeral high source port so it can open many connections at once. When a firewall log says 10.0.2.15:49152 → 93.184.216.34:443 tcp, the 443 is "web"; the 49152 is "this laptop's temporary door." Listening is not the same as connected. A LISTEN socket is a door waiting. ESTABLISHED (TCP) is a conversation in progress. UDP often shows UNCONN / no ESTABLISHED because there is no connection object in the TCP sense — only recent datagrams if the stack tracks them. If you do not separate those states, you will report "SSH is connected" when you only saw sshd listening.

2. TCP's three-way handshake — concept, not a packet weapon

TCP is connection-oriented. Before useful data, client and server agree they both exist and share starting sequence numbers. The classroom picture is three steps: (1) Client sends SYN ("I want to talk, here is my sequence start"). (2) Server replies SYN-ACK ("I heard you; here is mine"). (3) Client sends ACK. After that, both sides send a byte stream with sequence numbers, acknowledgements, and retransmissions if a segment goes missing. FIN/RST tear the session down. You do not need a raw-packet tool to use that picture in a SOC ticket. Reliability is the product: ordered bytes, loss recovery, flow control so a fast sender does not drown a slow receiver. The cost is latency (the handshake RTT), state on both ends, and head-of-line blocking when a lost segment stalls the stream. HTTP, HTTPS, SSH, SMTP, IMAP — the protocols you just studied — almost always ride TCP because a missing byte is a broken page, a truncated key exchange, or a chopped mail command.

Failure modes follow the state machine. SYN sent, no SYN-ACK: filtered or dead listener (or you probed an IP you should not have — don't). SYN-ACK then RST: the door slammed. Lots of SYNs, few ACKs: a flood, a scan, or a misconfigured client. A stateful firewall can allow ESTABLISHED,RELATED after seeing a good handshake, and drop stray ACK/data that never SYNed. That is a feature. It is also why "the ping works but the website does not" is not a paradox: ICMP is not TCP/443.

3. UDP: no handshake, DNS and video, firewalls that guess

UDP is connectionless. The application writes a datagram; the stack sends it. No three-way handshake, no automatic retransmission, no ordering guarantee. DNS queries are the everyday example: a small question, a small answer, UDP/53, often done in one round trip. If the answer is huge, DNS may retry over TCP — the exception that proves the rule. Live video, VoIP, and games prefer UDP (or UDP-based QUIC) because a late packet is worse than a missing one; they would rather skip a frame than stall the stream for TCP recovery. Security teams also know UDP as an amplifier's friend: a tiny spoofed query, a large response aimed at a victim. You will not run that. You will remember that "UDP is faster" is not the same as "UDP is harmless." On a host firewall, UDP is awkward because there is no ESTABLISHED handshake to hang state on. Devices track "we saw a recent outbound datagram to that IP:port, so inbound replies may return for a few seconds." Timeouts, direction, and asymmetric routing make UDP policy harder than TCP policy. Blocking UDP/53 "to be safe" looks like the internet died even though TCP/443 still works — you already met that with ping vs curl.

4. Wrong vs right: scanning others to "see SYN"

Worked failure mode — treating the handshake as a toy to fire at strangers. Read YOUR sockets. Do not nmap the LAN.

  • Wrong

    You write or download a SYN-flood / raw-socket "lab" and aim it at a classmate, a printer, or the school firewall to "watch drops." You nmap a /24 because UDP "needs more packets to be sure." You confuse a LISTEN line with proof that the internet can reach that port through NAT.

  • Right

    ss -tuln on your VM: TCP vs UDP, LISTEN vs everything else, 127.0.0.1 vs 0.0.0.0. Explain in one sentence why a DNS stub might show UDP/53 and a web server TCP/80. If you test a port, you test 127.0.0.1 on a listener you started. Handshake stays a diagram, not a weapon.

5. Practical: ss -tuln as a transport cheat sheet

ss talks to your kernel. -t TCP, -u UDP, -l listening, -n numeric ports (skip extra DNS). This is inventory of your machine, the same ethic as Topic 2. You will later map numbers to services; today, label each line: protocol, port, bind address, and whether TCP would have a handshake if a client connected locally.

Command guide

ss -tuln on your VM/WSL — no other hosts

Listening TCP and UDP, numeric ports

Command — copy this

ss -tuln

Split the view

Command — copy this

ss -tln          # TCP listen
ss -uln          # UDP listen

Notes

Command — copy this

mkdir -p "$HOME/cyberlium-lab"
{
  echo '=== ss -tuln ==='
  ss -tuln
  echo
  echo 'Pick 2 lines: protocol, port, bind (127.0.0.1 vs 0.0.0.0/*)'
  echo 'TCP listeners would 3-way handshake on connect; UDP would not.'
} > "$HOME/cyberlium-lab/tcp-udp-ss.txt"
chmod 600 "$HOME/cyberlium-lab/tcp-udp-ss.txt"

NEVER: nmap, hping, or raw SYN tools against other hosts NEVER: flood UDP at a printer "to test DDoS" NEVER: assume LISTEN on 127.0.0.1 is reachable from the café LAN

Mission: two listeners, TCP vs UDP, bind address

On your own VM or WSL, run ss -tuln. In $HOME/cyberlium-lab/tcp-udp-ss.txt pick two lines (ideally one TCP and one UDP if both exist). For each write: port, protocol, bind address, and whether a local client would perform a TCP three-way handshake. Do not scan other machines. Do not craft raw packets.

Stuck? Ask Cyberlium AI Mentor

If every line looks like UDP and you expected SSH, ask Cyberlium AI Mentor for a hint — not a SYN scanner. Try: "Hint only: ss -tuln shows 0.0.0.0:22 tcp LISTEN — what does LISTEN mean compared to ESTABLISHED, and who can reach 0.0.0.0 vs 127.0.0.1?"

You can treat ports as multiplexed doors, tell the TCP handshake as SYN / SYN-ACK / ACK without building a packet tool, and explain why DNS and video accept UDP's trade while SSH and HTTP generally cannot. Stateful firewalls hang TCP on a handshake; UDP gets a shorter, leakier memory. Next — Common Ports You Must Know — why 22, 53, 80, 443 and a short list of others are the numbers that show up in every triage, still interpreted with 127.0.0.1 vs 0.0.0.0.

Knowledge Check

1

APPLY: A log line is 10.0.2.15:53144 → 93.184.216.34:443 tcp flags SYN. What is 53144 doing, and what happens next if the server accepts?

Multiple choice

Knowledge Check

2

APPLY: Blocking outbound UDP/53 "for security" makes names fail while curl https://1.1.1.1/ still works. What did the firewall prove?

Multiple choice

Knowledge Check

3

APPLY: True or False: You should learn SYN/SYN-ACK/ACK as a diagram and confirm listeners with ss -tuln on your VM; you should not nmap neighbors to "practice the handshake."

True or False

← Previous

Answer all 3 knowledge checks to continue. (0/3 answered)