What you'll learn
- Explain ports and when UDP beats TCP
- Recognise well-known ports for common services
- Describe the TCP three-way handshake and what TCP adds over UDP
- Tell "connection refused" from a timeout when troubleshooting
Explain what ports are, how TCP gives reliable delivery, and when UDP is the better choice.
Why it matters
An IP address gets data to the right computer, but a server runs many programs at once: a web server, an SSH server, maybe a database. Ports get data to the right program. Choosing between TCPA connection-based transport protocol that numbers, acknowledges, orders and resends data for reliable delivery. and UDPA connectionless transport protocol that sends datagrams with no handshake, acknowledgement or resending. decides whether that data arrives reliably or quickly. Firewall rules, cloud security groups and "connection refused" errors are all about ports.
Ports
A portA 16-bit number (0 to 65535) at the transport layer that delivers data to the right program on a computer. is a 16-bit number (0 to 65535) at the transport layer. A connection is identified by five things: protocol, source IP, source port, destination IP, destination port.
- Well-known ports (0 to 1023) are used by standard services.
- Registered ports (1024 to 49151) are used by many applications, such as databases.
- Ephemeral portsTemporary high-numbered ports a client picks at random for its side of a connection. (usually 49152 to 65535) are picked at random by your computer for the client side of a connection.
Ports worth memorising:
| Port | Protocol | Service |
|---|---|---|
| 22 | TCP | SSH |
| 25 | TCP | SMTP (mail between servers) |
| 53 | UDP and TCP | DNS |
| 67/68 | UDP | DHCP |
| 80 | TCP | HTTP |
| 123 | UDP | NTP (time) |
| 443 | TCP (and UDP for HTTP/3) | HTTPS |
| 3306 | TCP | MySQL |
| 5432 | TCP | PostgreSQL |
| 3389 | TCP | Remote Desktop |
IANA keeps the official registry, but a port number is only a convention: you can run a web server on 8080. What matters is that client and server agree.
TCP: reliable, ordered, connection-based
TCP sets up a connection before sending data, with a three-way handshakeTCP's connection setup: the client sends SYN, the server replies SYN-ACK, and the client sends ACK.:
- Client sends SYN ("I want to connect, my sequence number starts at x").
- Server replies SYN-ACK.
- Client sends ACK. The connection is open.
After that, TCP numbers every byte, the receiver acknowledges what it got, and anything lost is resent. TCP also puts data back in order and slows down when the network is congested (flow and congestion control). The cost is extra round trips and waiting: one lost packet holds up everything behind it.
Use TCP when every byte must arrive: web pages, file transfers, email, SSH, database queries.
UDP: fast, simple, connectionless
UDP just sends datagrams. There is no handshake, no acknowledgement, no resending and no ordering. Its header is 8 bytes versus TCP's 20 or more.
UDP beats TCP when:
- Late data is useless. In a video call or online game, a resent packet from half a second ago is worse than a small glitch.
- Messages are tiny. A DNS question fits in one packet; a handshake would triple the time.
- The application handles reliability itself. QUIC, the protocol under HTTP/3, runs on UDP and adds its own faster reliability.
Worked example: two connections at once
You open two browser tabs to the same site. Your laptop (192.168.1.20) creates two TCP connections:
192.168.1.20:52814 -> 93.184.215.14:443
192.168.1.20:52815 -> 93.184.215.14:443The server tells them apart by the source port. Meanwhile your laptop asks DNS a question over UDP:
192.168.1.20:61002 -> 192.168.1.1:53 (UDP)Listening, open and closed
A server program listens on a port. If you connect to a port where nothing listens, the computer answers TCP with a reset and you see "connection refused". If a firewall silently drops the traffic, you see a timeout instead. That difference is a useful clue: refused means you reached the machine; timeout often means a firewall or routing problem.
Common mistakes
- Opening more ports than needed. Every listening port is attack surface. Expose only what a service needs, and only to the networks that need it (for example, a database port only to the app servers, never to the whole internet).
- Assuming DNS is UDP only. Large responses and zone transfers use TCP 53.
- Calling UDP "unreliable" as if it were broken. It is simply best-effort; for real-time traffic that is the right trade.
- Confusing the port with the protocol. HTTPS is usually on 443, but a port number does not guarantee what is running there.
Quick recap
- Ports (0 to 65535) deliver data to the right program; clients use random ephemeral ports.
- TCP: handshake, acknowledgements, retransmission, ordering. Reliable but slower.
- UDP: no handshake or retransmission. Fast and light, good for DNS, voice, video and games.
- "Refused" vs "timed out" tells you whether you reached the host.
Key terms (5)
- port
- A 16-bit number (0 to 65535) at the transport layer that delivers data to the right program on a computer.
- Ephemeral ports
- Temporary high-numbered ports a client picks at random for its side of a connection.
- three-way handshake
- TCP's connection setup: the client sends SYN, the server replies SYN-ACK, and the client sends ACK.
- TCP
- A connection-based transport protocol that numbers, acknowledges, orders and resends data for reliable delivery.
- UDP
- A connectionless transport protocol that sends datagrams with no handshake, acknowledgement or resending.