beginner

TCP, UDP and Ports

How data reaches the right application: port numbers and sockets, the TCP three-way handshake and reliability, why some traffic uses UDP instead, and how to see open ports on your own machine.

30 min

From host to application

An IP address gets a packet to the right device. But a device runs many network programs at once: a browser, an email client, a game, an update service. The transport layer gets data to the right program, using port numbers.

A port is a 16-bit number (0 to 65535). A program that wants to receive data listens on a port. The combination of IP address and port is a socket, and a connection is identified by four values: source IP, source port, destination IP, destination port.

Port ranges:

Range Name Use
0 to 1023 Well-known ports Standard services (assigned by IANA)
1024 to 49151 Registered ports Applications and vendors
49152 to 65535 Dynamic / ephemeral Temporary source ports chosen by clients

(Operating systems vary in the exact ephemeral range they use.)

Ports you should recognise:

Port Protocol Service
20, 21 TCP FTP (data, control)
22 TCP SSH (secure remote login)
23 TCP Telnet (insecure; avoid)
25 TCP SMTP (mail between servers)
53 UDP and TCP DNS
67, 68 UDP DHCP (server, client)
80 TCP HTTP
123 UDP NTP (time)
161 UDP SNMP
443 TCP (and UDP for HTTP/3) HTTPS
3389 TCP Remote Desktop

Two transport protocols

TCP: reliable, ordered streams

TCP (Transmission Control Protocol) is connection-oriented. It sets up a connection, numbers the bytes it sends, and requires acknowledgements. If something is lost, TCP resends it. If segments arrive out of order, TCP puts them back in order. It also adjusts its sending rate to avoid overwhelming the receiver (flow control) or the network (congestion control).

That reliability costs a little delay and overhead, and it is what web pages, email, file transfers and remote administration need.

The three-way handshake

Before data flows, TCP sets up the connection:

  1. SYN: the client sends a segment with the SYN flag and an initial sequence number.
  2. SYN-ACK: the server replies, acknowledging the client’s number and sending its own.
  3. ACK: the client acknowledges. The connection is established.
sequenceDiagram participant C as Client participant S as Server :443 C->>S: SYN S->>C: SYN-ACK C->>S: ACK C->>S: Data S->>C: ACK + Data

When finished, each side sends a FIN to close its direction (or a RST to abort). Firewalls use the flags to tell new connections from replies to existing ones, which is the basis of a stateful firewall.

UDP: fast and simple

UDP (User Datagram Protocol) is connectionless. It just sends a datagram with source and destination ports and a checksum. No handshake, no acknowledgements, no retransmission, no ordering. The sender does not know whether anything arrived.

UDP is the right choice when speed matters more than perfection, or when the application handles reliability itself:

  • DNS queries (tiny request, tiny reply; if it is lost, simply ask again)
  • Voice and video calls, live streaming and games (a late packet is useless)
  • DHCP, which has to work before the host has an address, and NTP
  • QUIC (the transport behind HTTP/3), which builds its own reliability on top of UDP
TCP UDP
Connection set-up Yes (handshake) No
Delivery guarantee Yes (retransmits) No
Ordering Yes No
Overhead / delay Higher Lower
Typical use Web, email, files, SSH DNS, voice, video, games

Seeing ports on your own computer

# Linux: listening and established sockets, with the owning process
ss -tulpn          # listening TCP/UDP sockets
ss -tn             # established TCP connections
# Windows
Get-NetTCPConnection -State Listen | Sort-Object LocalPort
netstat -ano

Run it with your browser open and find the connections to port 443. Notice the local side has a high random port and the remote side has 443.

To test whether a remote port is reachable from your machine:

Test-NetConnection example.com -Port 443
nc -vz example.com 443

Why ports matter for security

Every listening port is a way in. Good practice is to run only the services you need, close or firewall everything else, and never expose management ports (SSH 22, RDP 3389) directly to the internet without protection. A port scan simply tries many ports to see which answer; running one against systems you do not own or have no permission to test can be illegal.

A client connects to a web server over HTTPS. Describe the four values that identify the connection and what happens before the first byte of web data is sent.

The client’s IP and an ephemeral source port (for example 52814), and the server’s IP and destination port 443. Before data, TCP completes the three-way handshake (SYN, SYN-ACK, ACK). Then the TLS handshake takes place, and only after that is the encrypted HTTP request sent.

Can You Explain It?

Close the lesson and answer these without looking back. The goal isn't the wording — it's whether you've formed a usable mental model.

  • Explain what a port is and why a computer needs ports as well as an IP address.
  • Why does a video call prefer UDP while downloading a file prefers TCP?
  • Walk through the TCP three-way handshake and say what each message achieves.

🧪 Want to practise this on your own computer? See how to set up your lab →

🤖

Need clarification? Ask GT

Hi, I'm GT — your AI tutor for TCP, UDP and Ports. What would you like to know?
📝

Knowledge Check

1 of 4

Which is the correct order of the TCP three-way handshake?

Report a problem

About: /lessons/tcp-udp-and-ports