MarzleyTech Learn

Home / Learn / Networking / TCP, UDP and port numbers: reliable vs fast delivery, the handshake, sockets and ports to memorise

TCP, UDP and port numbers: reliable vs fast delivery, the handshake, sockets and ports to memorise

An IP address gets data to the right device. But your laptop might be running a browser, WhatsApp, Zoom, Spotify and a game at the same time. How does incoming data reach the right program? Port numbers do that, at the transport layer (layer 4). The transport layer also decides how data is delivered: carefully and reliably (TCP) or quickly with no guarantees (UDP).

Understanding TCP, UDP and ports is essential for configuring firewalls, troubleshooting "it connects but doesn't load" problems, setting up servers and passing networking and security exams.

What the transport layer does

JobMeaning
MultiplexingMany apps share one IP address using different ports
SegmentationBreak large data into segments that fit the network
Reliability (TCP)Detect and resend lost data, in order
Flow control (TCP)Don't send faster than the receiver can handle

TCP: Transmission Control Protocol

TCP is connection-oriented and reliable. Used for web pages, email, file transfers, SSH, database connections: anything where every byte must arrive correctly.

The three-way handshake

Before sending data, client and server set up a connection:

Client                        Server
  | ---- SYN (seq=100) ------->  |   "I want to connect; my numbering starts at 100"
  | <--- SYN-ACK (seq=300,      |   "OK; mine starts at 300; I got your 100"
  |       ack=101) ------------  |
  | ---- ACK (ack=301) ------->  |   "Got it"
  |      connection established  |

Reliable delivery

  • Each byte has a sequence number, so the receiver can put segments back in order.
  • The receiver sends acknowledgements (ACKs) for data received.
  • If an ACK doesn't arrive in time, the sender retransmits the segment.
  • A checksum detects corrupted segments.

Flow and congestion control

  • Window size: the receiver says how much data it can accept before the sender must wait.
  • Congestion control: TCP slows down when it detects packet loss (the network is congested) and speeds up gradually when things are clear. That's why downloads "ramp up" at the start.

Closing a connection

Each side sends FIN and receives an ACK (a four-step close). A RST (reset) abruptly ends a connection, e.g. when a port is closed.

UDP: User Datagram Protocol

UDP is connectionless: no handshake, no acknowledgements, no retransmission, no ordering. Each datagram is sent and forgotten. Its header is tiny (8 bytes vs TCP's 20+).

Why use something unreliable? Because for some applications, late data is useless: in a live video call, a resent packet from 2 seconds ago only causes lag. It's better to skip it and keep going. Applications that need some reliability over UDP build their own (for example QUIC, used by HTTP/3, adds reliability and encryption on top of UDP).

TCP vs UDP

TCPUDP
ConnectionYes (handshake)No
ReliabilityGuaranteed, orderedBest effort
Speed and overheadSlower, bigger headersFaster, small header
Flow/congestion controlYesNo (app's job)
Typical usesWeb (HTTP/1.1, HTTP/2), email, SSH, file transfer, databasesVideo/voice calls, live streaming, online games, DNS queries, DHCP, VPNs like WireGuard, HTTP/3 (QUIC)

Port numbers

A port is a 16-bit number: 0 to 65,535.

RangeNameUse
0–1023Well-known portsStandard services (HTTP 80, HTTPS 443); need admin rights to listen on
1024–49151Registered portsApplications (MySQL 3306, RDP 3389)
49152–65535Dynamic/ephemeralTemporary client-side ports picked by your OS for each connection

Ports to memorise

PortProtocolTransportService
20, 21FTPTCPFile transfer (data, control)
22SSH / SFTPTCPSecure remote login, secure file transfer
23TelnetTCPInsecure remote login (avoid)
25SMTPTCPSending email between servers
53DNSUDP (and TCP)Name lookups
67, 68DHCPUDPAddress assignment (server 67, client 68)
69TFTPUDPSimple file transfer (network device configs)
80HTTPTCPWeb
110POP3TCPDownloading email
123NTPUDPTime synchronisation
143IMAPTCPEmail access
161, 162SNMPUDPNetwork monitoring
389LDAPTCPDirectory services
443HTTPSTCP (and UDP for HTTP/3)Secure web
445SMBTCPWindows file sharing
465, 587SMTPS / SubmissionTCPSending email from apps/clients
993, 995IMAPS, POP3STCPSecure email access
3306MySQL/MariaDBTCPDatabase
3389RDPTCPWindows Remote Desktop
5432PostgreSQLTCPDatabase
8080HTTP alternateTCPTest/proxy web servers

Sockets: the full address of a conversation

A socket is an IP address + port, e.g. 192.168.1.20:51515. A connection is identified by four values: source IP, source port, destination IP, destination port.

When you open two tabs to the same website, your computer uses two different source ports (e.g. 51515 and 51516) to the same destination 93.184.216.34:443, so replies reach the right tab.

Python · runs live in the interactive lesson
# A connection is identified by four values
connections = [
    ("192.168.1.20", 51515, "93.184.216.34", 443),
    ("192.168.1.20", 51516, "93.184.216.34", 443),
    ("192.168.1.20", 50001, "8.8.8.8", 53),
]
well_known = {443: "HTTPS", 53: "DNS", 22: "SSH", 80: "HTTP"}
for src, sport, dst, dport in connections:
    print(f"{src}:{sport} -> {dst}:{dport} ({well_known.get(dport, '?')})")

Ports, NAT and firewalls

  • Home routers (NAT) track connections by port so replies return to the right device inside. Port forwarding opens a specific port (e.g. TCP 8000 → the CCTV recorder) so outside users can connect in.
  • Firewalls allow or block traffic by protocol and port: "allow TCP 443 from anywhere to the web server; allow TCP 22 only from the office IP; block everything else".
  • An open port has a program listening; a closed port replies with RST; a filtered port is silently dropped by a firewall.

Seeing connections on your computer

Terminal
netstat -ano            # Windows: all connections with process IDs
ss -tunap               # Linux: TCP/UDP connections and processes
ss -tulpn               # Linux: listening ports
Test-NetConnection example.com -Port 443   # Windows PowerShell: is the port reachable?
nc -zv example.com 443  # Linux/macOS: test a TCP port

States you'll see: LISTEN (waiting for connections), ESTABLISHED (active), TIME_WAIT (recently closed), SYN_SENT (trying to connect: if stuck here, something is blocking).

Think about it: A new web server works when you browse to it from the server itself, but nobody outside can load the website. ss -tulpn shows nginx listening on 0.0.0.0:443. What should you check next?Show answer

The service is listening on all addresses, so check the path: the server's firewall (ufw status must allow 443), the cloud provider's security group/firewall, any router port forwarding, and DNS pointing to the right IP. Test from outside with Test-NetConnection or nc -zv.

Summary

  • The transport layer delivers data to the right application using ports and chooses TCP or UDP.
  • TCP: handshake (SYN, SYN-ACK, ACK), sequence numbers, ACKs, retransmission, flow and congestion control.
  • UDP: no connection, no guarantees, low overhead; ideal for real-time media, DNS, DHCP, games, QUIC.
  • Ports 0–65535: well-known (0–1023), registered, ephemeral; memorise the common ones.
  • A connection = source IP + port + destination IP + port; NAT and firewalls work with ports.

Check yourself

  1. What are the three steps of the TCP handshake? (write like SYN, SYN-ACK, ACK)

    Show answer

    SYN, SYN-ACK, ACK

  2. Which port does HTTPS use?

    Show answer

    443

  3. Which port does SSH use?

    Show answer

    22

  4. Does DNS mainly use TCP or UDP?

    Show answer

    UDP

  5. Which protocol is better for a live video call, TCP or UDP?

    Show answer

    UDP

  6. Which port does MySQL use by default?

    Show answer

    3306

  7. Which ports does DHCP use? (server, client)

    Show answer

    67, 68

  8. Which port does Windows Remote Desktop (RDP) use?

    Show answer

    3389

Lesson 13 of 24 in Networking · Printable course notes