The door numbers on a machine, and the connections that pass through them.
An IP address finds the right computer. A port finds the right program on that computer. A socket is one end of a live connection between two programs.
One computer runs many network programs at once: a web server, a database, an SSH service, a mail program. When a packet arrives, the computer must decide which program should get it. The IP address only gets the packet to the machine. The port number picks the program.
A port is just a 16-bit number, so it goes from 0 to 65535. A program that wants to receive connections asks the operating system to listen on a port. Only one program can listen on a given port (and protocol) at a time on the same address. A second one gets an error such as "Address already in use".
A socket is the operating system's handle for one end of a network conversation. A server has a listening socket that waits for new clients. Each time a client connects, the server gets a new connected socket for that one client. The listening socket keeps waiting for the next one.
| Range | Name | Use |
|---|---|---|
| 0 to 1023 | Well-known ports | Standard services: 80 (HTTP), 443 (HTTPS), 22 (SSH), 53 (DNS), 25 (SMTP). On many systems only privileged programs may listen here. |
| 1024 to 49151 | Registered ports | Applications registered by convention: 3306 (MySQL), 5432 (PostgreSQL), 6379 (Redis), 8080 (alternative HTTP). |
| 49152 to 65535 | Dynamic or ephemeral ports | Temporary source ports for clients (in the standard suggested range). Real systems may use a different range. |
TCP port 53 and UDP port 53 are different doors. DNS uses both. A program listening on TCP 8080 does not block another from using UDP 8080. See TCP vs UDP for how the two differ.
A simplified picture. The addresses are from ranges reserved for examples. The ephemeral ports (49152 and above) come from a fixed formula, so the same settings always show the same ports. A real operating system may use a different range.
# who is listening on port 443? sudo lsof -i :443 ss -ltnp | grep 443 # test whether a port is reachable nc -vz example.com 443
| Term | Meaning in easy words |
|---|---|
| Port | A number (0 to 65535) that picks a program on a machine |
| Socket | One end of a network conversation, as seen by the operating system |
| Listening socket | A socket that waits for new clients on a port |
| Connected socket | A socket for one live connection with one client |
| Well-known port | A standard port for a common service, such as 80, 443 or 22 |
| Ephemeral port | A temporary source port the OS gives to an outgoing connection |
| Five-tuple | Protocol, source IP, source port, destination IP and destination port. It names one connection |
| Bind | Ask the OS to attach a socket to an address and port |
| Port exhaustion | A machine runs out of free source ports for new outgoing connections |
| TIME_WAIT | A short state after a TCP connection closes, when the OS keeps the port pair reserved so late packets cannot confuse a new connection |
| Address already in use | The error when another program already listens on that address and port |
| Choice | We gain | We lose |
|---|---|---|
| Standard port (443) | Clients and firewalls just work | Scanners and attackers know where to look |
| Custom port | Avoids clashes, hides from casual scans | Must be documented, and some networks block unusual ports |
| Listen on 0.0.0.0 | Reachable from other machines | Exposed to every network the host is on |
| Listen on 127.0.0.1 | Safe: only local programs connect | Not reachable from outside |
| Many short connections | Simple code | More ports used, more handshakes, more TIME_WAIT |
| Reused (keep-alive) connections | Fewer ports, faster requests | Idle connections hold resources and need timeouts |