Introduction: IP Address and Port Number
Delivering data to a process requires identifying both a host and a service endpoint. An IP address identifies the host on the network, while a port number identifies a service or socket on that host. Without a port number, the operating system would not know which of many network processes should receive the incoming data.
In computer networks, the Network Layer, using the , delivers packets to the correct host based on the IP address. After a packet arrives, the operating system must select the destination process. A computer can run a browser, email client, music-streaming service, video game, and other applications at the same time, so the IP address alone cannot perform demultiplexing.
The Transport Layer, especially TCP or UDP, solves this problem with port numbers. A port number is a 16-bit logical identifier for the endpoint to which the operating system delivers data for a particular service or process.
What Exactly Is a Port Number?
A port number is a 16-bit unsigned integer, so it ranges from 0 to 65,535 . It serves as a logical, numbered communication endpoint managed by the operating system.
When an application wants to send or receive data over the network, it requests a port from the operating system. The operating system then creates a communication endpoint known as a , which commonly combines the computer’s IP address, transport protocol, and assigned port number. For example, if a computer with IP address has a web server on port 80, the endpoint can be written as . For TCP, a pair of such endpoints together with the protocol identifies the connection.
This combination allows precise delivery. TCP and UDP headers contain two port numbers:
- Source Port: The port number of the application on the sending machine.
- Destination Port: The port number of the application on the receiving machine.
The port pair, considered together with the IP addresses and transport protocol, distinguishes sessions. A single computer can therefore handle thousands of simultaneous network conversations.
A port number belongs to the namespace of a particular transport protocol, so the same value can have separate meanings for TCP and UDP. Local configuration can also associate a different service with a number than the default registry entry.
The Core Function: Demultiplexing
The primary job of port numbers is to support demultiplexing. Multiplexing combines data from multiple applications into traffic handled by a shared network interface. Demultiplexing separates received data according to transport-header information and delivers it to the correct sockets.
During sending, multiple applications share a network interface while the transport layer adds source and destination ports to their data. During reception, the system reads the destination port and selects the socket to which it will deliver the data.
In networking, the computer’s network interface receives packets from the network, and the operating system’s Transport Layer examines the destination port number in each header. It uses that value to select the application socket and deliver the packet data. Multiple applications can share one network interface without mixing their streams.
Port Number Categories
The 65,536 possible port values are organized by the IANA registry. The maintains assignments in three categories. The categories organize service names and default values used by TCP and UDP protocols.
System Ports
0 - 1023
An IANA-managed range for assignments used by system services and widely used protocols. The number is a service convention, not a guarantee that a particular process is running.
User Ports
1024 - 49151
A range in which IANA can assign numbers to specific services. Some values remain unassigned, and operating systems do not enforce registry use.
Dynamic / Private Ports
49152 - 65535
A range never assigned by IANA, intended for private and temporary use. It often supplies source ports for client connections.
System Ports (Well-Known Ports)
IANA manages assignments from 0 to 1023 for system services and well-known protocols. A browser commonly selects port 80 for HTTP or 443 for HTTPS according to the service convention. A conforming client can therefore contact a server without first negotiating a port, although the number alone does not guarantee that the service is running.
On many operating systems such as Linux or macOS, an application needs special administrative privileges, for example running as root, to listen on ports below 1024. This is a local system rule that limits the ability of programs to impersonate system services.
Examples of Well-Known Ports:
- 20 and 21 (FTP): File Transfer Protocol, for transferring files.
- 22 (SSH): Secure Shell, for secure remote command-line access.
- 25 (SMTP): Simple Mail Transfer Protocol, for sending emails.
- 53 (DNS): Domain Name System, for translating domain names into IP addresses.
- 80 (HTTP): Hypertext Transfer Protocol, the foundation of the World Wide Web.
- 443 (HTTPS): HTTP secured with TLS.
User Ports (Registered Ports)
The range from 1024 to 49151 covers user services that may need a stable, recognizable port number. Designers can request an IANA assignment for a specific service, while some values remain unassigned. The registry organizes service names and default assignments; an operating system does not enforce local compliance with it.
An IANA assignment describes the default service, while an application can use another free port after local configuration. The client then needs to learn that number from configuration or the service documentation.
Examples of Registered Ports:
- 1433 (MS SQL Server): For Microsoft's database server.
- 3306 (MySQL): For the popular open-source database server.
- 3389 (RDP): Remote Desktop Protocol, for graphical remote control of Windows machines.
- 5432 (PostgreSQL): For another major open-source database server.
- 8080 (HTTP Alternate): Often used as an alternative port for web servers during development or when port 80 is unavailable.
Dynamic, Private, or Ephemeral Ports
The range from 49152 to 65535 is never assigned by IANA and is intended for dynamic, private, or temporary use. These are known as ephemeral ports and often serve as source ports for client connections.
When a browser visits a site, it acts as the client and the web server usually listens on a well-known port such as 443. The operating system assigns the browser a port for the local end of the connection by selecting an unused value from its local pool of temporary ports. This is called ephemeral allocation.
The port remains associated with the connection for its lifetime. A new tab may reuse an existing HTTP/2 or HTTP/3 connection, or receive a new port when a separate connection is required. This mechanism lets one machine maintain hundreds of outgoing connections. The server distinguishes them using the IP addresses, ports, and transport protocol.
Ports in Action: An Example
The following sequence shows how IP addresses and port numbers participate in a simple web request.
With HTTP/3, the initial exchange uses a QUIC connection over UDP instead of a TCP SYN, often still with destination port 443. The establishment details then depend on the transport used by the client and server.
Scenario: A user on a home computer wants to visit the secure website https://www.example.com.
- Initiation: The user types the address into their browser and hits Enter. The browser, acting as the client application, knows that HTTPS corresponds to the well-known port 443.
- DNS Lookup: The browser asks the operating system to find the IP address for
www.example.com. The OS usually sends a DNS query over UDP to a server on port 53, though DNS can also use TCP. It receives an IP address, for instance, . - Ephemeral Port Allocation: The browser now needs to make a connection. The operating system allocates a free port from the dynamic range for the browser to use as its source port. Let's say it picks port .
- Packet Creation:If the HTTPS connection uses TCP, the client machine creates the first TCP packet, the SYN that starts the connection. The packet's header will contain:
- Source IP: The user's public IP address.
- Source Port: (the ephemeral port).
- Destination IP: (the server's IP).
- Destination Port: (the well-known HTTPS port).
- Server-Side Demultiplexing: The packet travels across the internet and arrives at the example.com server. The server’s operating system looks at the destination port () and delivers the packet to the web server application, which is actively listening for new connections on that port.
- The Response: The web server accepts the connection and sends a response packet (SYN/ACK). The header of this response packet will have the source and destination information swapped:
- Source IP: .
- Source Port: .
- Destination IP: The user's public IP address.
- Destination Port: .
- Client-Side Demultiplexing: The response packet arrives back at the user's computer. The operating system looks at the destination port () and knows that this packet is part of the conversation initiated by the browser. It delivers the data directly to the correct browser process, which can then start rendering the webpage.