Sub topic 1
What a Port Number Identifies
Prerequisites
Assume you know what an IP address identifies and understand the client-server model. You should also be comfortable running terminal commands and reading short Python scripts.
What a Port Number Identifies
A port number identifies a network endpoint for a transport protocol. It lets one IP address provide many services at the same time. For example, a server might accept web traffic on TCP port 80 and secure web traffic on TCP port 443.
A connection endpoint is identified by an IP address, a port number, and a transport protocol such as TCP or UDP. TCP port 80 and UDP port 80 are different endpoints.
Well-Known, Registered and Dynamic Port Ranges
Port numbers range from 0 through 65535.
| Range | Common name | Typical use |
|---|---|---|
| 0–1023 | Well-known | Core and widely recognized services |
| 1024–49151 | Registered | Applications with registered service names |
| 49152–65535 | Dynamic or private | Temporary client ports |
Operating systems may choose ephemeral, or temporary, ports from a different local range. Check the operating system configuration rather than assuming an exact range.
TCP and UDP: Which Protocols Use Which
TCP provides a reliable, ordered byte stream. It establishes a connection, retransmits lost data, and controls its sending rate. HTTP, SSH, SMTP, and IMAP commonly use TCP.
UDP sends independent datagrams without establishing a reliable stream. It has less overhead and supports fast request-response traffic. DNS commonly uses UDP, although it can use TCP when responses are large or reliability requires it.
Sockets and Ephemeral Client Ports
A socket is an operating-system communication endpoint. A listening server socket binds to a local IP address, port, and protocol. When a client connects, the operating system usually assigns the client an ephemeral source port.
A TCP connection is commonly distinguished by four values: client IP and port, plus server IP and port. TCP also belongs to its own protocol namespace, so the same numeric port can exist independently for UDP.
Where Port Assignments Come From
IANA, the Internet Assigned Numbers Authority, maintains the service-name and port-number registry. A local system also stores commonly known mappings, often in /etc/services. These mappings are conventions, not proof that a service is actually running.
import socket
for name in ("http", "https", "ssh", "domain"):
print(name, socket.getservbyname(name, "tcp"))
print(socket.getservbyport(443, "tcp"))Exercise
A client connects to a web server at TCP port 443. Which port is usually ephemeral?
Show answer
The client's source port is usually ephemeral; the server's destination port is 443.