Loading content...
IP addresses get packets to the correct host. But a host runs many applications simultaneously—web servers, databases, email clients, streaming services. Once a packet reaches the host, how does the operating system know which application should receive it?
The answer lies in port numbers—16-bit identifiers that extend addressing from the host level down to the process level. Port numbers are the essential mechanism that enables multiplexing and demultiplexing; without them, each host could only run one network application at a time.
The Complete Network Address:
Full Transport Layer Address = IP Address + Port Number
= 192.168.1.100:443
= Host Address + Process Address
This combination—often written as IP:Port—uniquely identifies a network endpoint on the entire Internet.
By the end of this page, you will understand the complete port identification system—how 65,536 available ports are categorized, how ports are assigned dynamically and statically, how both TCP and UDP maintain separate port namespaces, and how port numbers flow through the multiplexing and demultiplexing processes. You'll gain practical knowledge of common port numbers and understand the security implications of port usage.
What is a Port Number?
A port number is a 16-bit unsigned integer (ranging from 0 to 65535) that identifies a specific process or service on a host. Port numbers are carried in the transport layer header—both TCP and UDP include source and destination port fields.
Technical Specifications:
Key Properties:
12345678910111213141516171819
TCP Header (first 4 bytes):┌─────────────────────────────────────────────────────┐│ Source Port (16 bits) │ Destination Port (16 bits) │├─────────────────────────────────────────────────────┤│ Bits 0-15: Sender's port │ Bits 16-31: Receiver's port │└─────────────────────────────────────────────────────┘ UDP Header (first 4 bytes):┌─────────────────────────────────────────────────────┐│ Source Port (16 bits) │ Destination Port (16 bits) │├─────────────────────────────────────────────────────┤│ Bits 0-15: Sender's port │ Bits 16-31: Receiver's port │└─────────────────────────────────────────────────────┘ Examples:- HTTP request: Source=52431, Destination=80- HTTP response: Source=80, Destination=52431 (reversed)- DNS query: Source=51234, Destination=53- DNS response: Source=53, Destination=51234 (reversed)The 16-bit port size (65,536 values) was chosen in the early 1980s as a balance between header overhead and capability. With modern multi-tenant systems running thousands of connections, this limit occasionally becomes a bottleneck. Some applications work around this via connection pooling or techniques like SO_REUSEPORT.
Source vs. Destination Ports:
Understanding the role of each port field is critical:
Source Port:
Destination Port:
Bidirectional Communication:
When a response is sent, the source and destination ports swap:
Client → Server: Src=52431, Dst=80
Server → Client: Src=80, Dst=52431
This swapping is how stateless protocols (like UDP) can send responses without maintaining connection state.
The Internet Assigned Numbers Authority (IANA) divides the 65,536 available ports into three distinct ranges, each with different purposes and assignment rules.
Range 1: Well-Known Ports (0-1023)
Range 2: Registered Ports (1024-49151)
Range 3: Dynamic/Private/Ephemeral Ports (49152-65535)
| Range | Name | Ports | Count | Binding Privileges | Primary Use |
|---|---|---|---|---|---|
| 1 | Well-Known | 0-1023 | 1,024 | Root/Admin required | Standard protocols |
| 2 | Registered | 1024-49151 | 48,128 | No special privileges | User applications |
| 3 | Dynamic | 49152-65535 | 16,384 | No special privileges | Ephemeral client ports |
The privilege requirement for well-known ports provides basic security: only root/admin can run services on these ports. This means if you connect to port 22 (SSH), you can be reasonably confident that an authorized system service is running there, not a rogue user process. However, this protection doesn't exist for higher ports.
Operating System Ephemeral Port Ranges:
Different operating systems define ephemeral ranges differently:
| Operating System | Ephemeral Range | Port Count |
|---|---|---|
| Linux (modern) | 32768-60999 | 28,232 |
| Windows (Vista+) | 49152-65535 | 16,384 |
| macOS | 49152-65535 | 16,384 |
| FreeBSD | 49152-65535 | 16,384 |
| Linux (old) | 32768-61000 | 28,233 |
Checking Your System's Range:
# Linux
cat /proc/sys/net/ipv4/ip_local_port_range
# Windows
netsh int ipv4 show dynamicport tcp
# macOS
sysctl net.inet.ip.portrange
Port Exhaustion:
When a client makes many simultaneous outbound connections, it can exhaust available ephemeral ports. With 16,384 ephemeral ports and TCP's TIME_WAIT (typically 60-120 seconds), a busy client might hit limits when making more than ~100-200 connections per second to the same destination.
Well-known ports (0-1023) are the most critical for understanding network services. These ports have been assigned to fundamental Internet protocols, and knowing them is essential for network administration, security analysis, and application development.
Port 0: Reserved
Essential Well-Known Ports:
| Port | Protocol | Service | Transport | Description |
|---|---|---|---|---|
| 20 | FTP-Data | File Transfer | TCP | FTP data transfer channel |
| 21 | FTP | File Transfer | TCP | FTP control channel |
| 22 | SSH | Secure Shell | TCP | Encrypted remote access |
| 23 | Telnet | Remote Terminal | TCP | Unencrypted remote access (deprecated) |
| 25 | SMTP | Simple Mail Transfer | TCP | Email sending |
| 53 | DNS | Domain Name System | TCP/UDP | Name resolution |
| 67 | DHCP Server | Dynamic Host Config | UDP | IP address assignment (server) |
| 68 | DHCP Client | Dynamic Host Config | UDP | IP address assignment (client) |
| 80 | HTTP | Web Traffic | TCP | Unencrypted web |
| 110 | POP3 | Post Office Protocol | TCP | Email retrieval |
| 123 | NTP | Network Time Protocol | UDP | Time synchronization |
| 143 | IMAP | Internet Message Access | TCP | Email access |
| 443 | HTTPS | Secure Web | TCP | Encrypted web (TLS) |
| 445 | SMB | Server Message Block | TCP | Windows file sharing |
| 993 | IMAPS | Secure IMAP | TCP | Encrypted email access |
| 995 | POP3S | Secure POP3 | TCP | Encrypted email retrieval |
Port Number Memorization Framework:
Certain port numbers follow logical patterns:
The Special Case of Port 443:
Port 443 (HTTPS) has become arguably the most important port on the Internet:
DNS (port 53) is notable for using both TCP and UDP. Standard queries use UDP for speed (single request/response). TCP is used for zone transfers between DNS servers and for responses exceeding 512 bytes (or 4096 with EDNS). This dual-protocol usage is rare but important to understand.
Registered Ports (1024-49151):
Registered ports are available for services and applications that don't warrant a well-known port but benefit from a consistent, documented port number. IANA maintains a registry of these assignments.
Common Registered Ports:
| Port | Service | Category | Notes |
|---|---|---|---|
| 1433 | Microsoft SQL Server | Database | Default SQL Server instance |
| 1521 | Oracle Database | Database | Oracle TNS Listener |
| 3306 | MySQL | Database | Default MySQL port |
| 3389 | RDP | Remote Access | Windows Remote Desktop |
| 5432 | PostgreSQL | Database | Default PostgreSQL port |
| 5672 | AMQP | Messaging | RabbitMQ and similar |
| 6379 | Redis | Database | Redis in-memory store |
| 8080 | HTTP Alternate | Web | Common alternative HTTP |
| 8443 | HTTPS Alternate | Web | Common alternative HTTPS |
| 9000 | Various | Application | Popular for dev servers |
| 27017 | MongoDB | Database | Default MongoDB port |
Dynamic/Ephemeral Ports (49152-65535):
Ephemeral ports are the workhorses of client-side networking. Every time an application makes an outbound connection, the operating system assigns an ephemeral port as the source port.
Ephemeral Port Lifecycle:
1. Application calls connect() or sendto()
2. OS selects next available ephemeral port (e.g., 52341)
3. Port is marked as in-use in the port allocation table
4. Connection uses this port for all communication
5. Connection closes (or UDP exchange completes)
6. Port enters TIME_WAIT (TCP) or is released (UDP)
7. After TIME_WAIT expires, port is available for reuse
Port Allocation Algorithms:
Sequential: Allocate ports in order (52341, 52342, 52343...)
Random: Select randomly from available ports
Round-Robin with Randomization: Hybrid approaches
If a server service accidentally binds to a port in the ephemeral range, it may conflict with dynamically assigned client ports. This is why services should use well-known or registered ports, not ephemeral ports. Some services allow configuration of the port to avoid conflicts.
Understanding how ports are assigned is crucial for both system administration and application development. There are two primary mechanisms: explicit binding and implicit assignment.
Explicit Binding (Server Pattern):
Servers explicitly request a specific port:
server_socket = socket(AF_INET, SOCK_STREAM, 0)
server_socket.bind(('0.0.0.0', 80)) # Explicitly request port 80
server_socket.listen(backlog)
This is the server pattern: bind to a known port so clients can find you.
Implicit Assignment (Client Pattern):
Clients let the OS assign an ephemeral port:
client_socket = socket(AF_INET, SOCK_STREAM, 0)
# No explicit bind() - OS assigns port on connect()
client_socket.connect(('example.com', 80))
# OS has now assigned an ephemeral source port
This is the client pattern: you don't care which local port you use.
Socket Options Affecting Port Assignment:
SO_REUSEADDR:
Allows binding to a port that's in TIME_WAIT state:
server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
server_socket.bind(('0.0.0.0', 80))
Useful for server restarts—without this, you'd wait for TIME_WAIT to expire.
SO_REUSEPORT:
Allows multiple sockets to bind to the same port:
# Multiple processes can share port 80
server_socket.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
server_socket.bind(('0.0.0.0', 80))
Used for load balancing across multiple server processes.
Binding to Specific Interfaces:
bind(('192.168.1.100', 80)) # Only reachable via 192.168.1.100
bind(('127.0.0.1', 80)) # Only reachable via localhost
bind(('0.0.0.0', 80)) # Reachable via any interface
bind(('::', 80)) # IPv6 any interface
"Address already in use" is one of the most common socket errors. It occurs when attempting to bind to a port that's already bound (another process is using it) or in TIME_WAIT (previous connection hasn't fully closed). Use netstat/ss to diagnose, and consider SO_REUSEADDR for legitimate restarts.
We've discussed ports in isolation; now let's see exactly how they function within the multiplexing and demultiplexing framework.
At the Sender (Multiplexing):
Ports identify which socket's data is being sent:
Socket A (port 52341) sends data to server:443
→ Segment header: Src=52341, Dst=443
Socket B (port 52342) sends data to server:443
→ Segment header: Src=52342, Dst=443
Socket C (port 52343) sends data to different:80
→ Segment header: Src=52343, Dst=80
The source port uniquely identifies the sending socket. The destination port indicates the target service.
At the Receiver (Demultiplexing):
Ports determine which socket receives the data:
Segment arrives: Src=52341, Dst=443
→ Look up socket bound to 443 (with matching 4-tuple for TCP)
→ Deliver to that socket's receive buffer
Segment arrives: Src=52342, Dst=443
→ Different source port = different connection
→ Different socket receives this segment
1234567891011121314151617181920212223242526272829
CLIENT (192.168.1.10) SERVER (203.0.113.50)═══════════════════════ ═══════════════════════ Application Layer Application Layer├─ Browser (socket fd=5) ├─ Web Server (listen fd=4)│ Local port: 52341 │ Local port: 443│ ││ send("GET /index.html") │ Connection sockets:│ ↓ │ ├─ fd=6: (192.168.1.10:52341)│ │ └─ fd=7: (192.168.1.10:52342)├────────────────────────────────────────────────────────────────│Transport Layer (Multiplexing) Transport Layer (Demultiplexing)├─ Extract data from socket fd=5 ├─ Receive segment├─ Get source port: 52341 ├─ Extract Dst port: 443├─ Set destination port: 443 ├─ Extract Src port: 52341├─ Create segment header ├─ Build 4-tuple├─ Pass to Network Layer ├─ Look up matching socket: fd=6│ ↓ │ Deliver to fd=6 buffer├────────────────────────────────────────────────────────────────│Network Layer Network Layer ├─ Add IP header ├─ Verify IP destination├─ Src IP: 192.168.1.10 ├─ Extract TCP segment├─ Dst IP: 203.0.113.50 ├─ Pass to Transport Layer│ ↓ │ ↑├────────────────────────────────────────────────────────────────│ │└──────────── Network Transmission ────────┘The Complete Identification Chain:
| Layer | Identification Purpose | Identifier |
|---|---|---|
| Application | Process/Service | Socket file descriptor |
| Transport | Port-level endpoint | Port number |
| Network | Host on internet | IP address |
| Link | Device on local network | MAC address |
Each layer adds its own identification. Port numbers are the transport layer's contribution—enabling host-level multiplexing and demultiplexing.
Port Identification Table:
The operating system maintains a table mapping ports to sockets:
TCP Port Table:
┌─────────────────────────────────────────────────────────────┐
│ Local Addr │ Local Port │ Remote Addr │ Remote Port │ Socket │
├─────────────────────────────────────────────────────────────┤
│ 0.0.0.0 │ 80 │ * │ * │ LISTEN │
│ 0.0.0.0 │ 443 │ * │ * │ LISTEN │
│ 192.168.1.10 │ 52341 │ 203.0.113.50 │ 443 │ ESTAB │
│ 192.168.1.10 │ 52342 │ 203.0.113.50 │ 443 │ ESTAB │
│ 192.168.1.10 │ 52343 │ 10.0.0.1 │ 22 │ ESTAB │
└─────────────────────────────────────────────────────────────┘
Demultiplexing consults this table for every incoming segment.
A critical but often misunderstood concept: TCP and UDP have completely separate port namespaces. TCP port 53 is entirely different from UDP port 53.
Protocol Number Differentiation:
In the IP header, the protocol field (field 10) indicates which transport protocol the payload contains:
When a segment arrives, the IP layer first checks this protocol field, then passes the segment to the appropriate transport protocol handler. This is why the same port number can be used by both TCP and UDP simultaneously.
Example: DNS Uses Both Protocols on Port 53:
DNS Query (UDP): Protocol=17, Dst Port=53 → UDP port 53 socket
DNS Query (TCP): Protocol=6, Dst Port=53 → TCP port 53 socket
These are different sockets, potentially different processes!
A server can (and often does) listen on the same port number for both TCP and UDP. A DNS server binds to both TCP:53 and UDP:53. The kernel distinguishes them by protocol, and incoming packets are demultiplexed to the correct socket based on the IP protocol field before the port is even considered.
Practical Implications:
Port scanning must check both protocols: A port may be open on TCP but closed on UDP (or vice versa)
Firewall rules are protocol-specific: Allowing TCP:80 doesn't allow UDP:80
Services may use different protocols on the same port: DNS uses UDP for queries but TCP for zone transfers, both on port 53
Socket programming requires explicit protocol selection: You must specify TCP or UDP when creating a socket
Socket Creation Example:
# These create sockets in different namespaces
tcp_socket = socket(AF_INET, SOCK_STREAM, 0) # TCP
udp_socket = socket(AF_INET, SOCK_DGRAM, 0) # UDP
# Both can bind to port 53 simultaneously
tcp_socket.bind(('0.0.0.0', 53)) # TCP port 53
udp_socket.bind(('0.0.0.0', 53)) # UDP port 53 - no conflict!
Viewing Both Namespaces:
# Linux: Show TCP listening ports
ss -tln
# Linux: Show UDP listening ports
ss -uln
# Windows: Show both
netstat -an
| Port | TCP Usage | UDP Usage | Notes |
|---|---|---|---|
| 53 | DNS (zone transfers, large responses) | DNS (standard queries) | Most DNS is UDP |
| 88 | Kerberos authentication | Kerberos authentication | Both commonly used |
| 123 | NTP (rarely) | NTP (standard) | Almost always UDP |
| 161 | SNMP (rarely) | SNMP (standard) | Almost always UDP |
| 443 | HTTPS | QUIC/HTTP/3 | UDP becoming common |
| 500 | IKE key exchange | IKE key exchange | Both used in IPSec |
Port usage has significant security implications. Understanding these is essential for network security.
Port Scanning:
Attackers scan ports to discover running services:
TCP SYN Scan:
Send SYN to each port
→ SYN-ACK response = Port open (service running)
→ RST response = Port closed (no service)
→ No response = Port filtered (firewall blocked)
UDP Scan:
Send UDP packet to each port
→ ICMP Port Unreachable = Port closed
→ Application response = Port open
→ No response = Open or filtered (ambiguous)
Mitigation Strategies:
Port Redirection and NAT:
Network Address Translation (NAT) translates port numbers as packets traverse network boundaries:
Internal: 192.168.1.100:52341 → External: 203.0.113.1:61234
NAT table maintains mapping:
┌─────────────────────────────────────────────────────┐
│ Internal IP:Port │ External IP:Port │ Dest │
├─────────────────────────────────────────────────────┤
│ 192.168.1.100:52341 │ 203.0.113.1:61234 │ google:443│
│ 192.168.1.101:52341 │ 203.0.113.1:61235 │ google:443│
└─────────────────────────────────────────────────────┘
Multiple internal hosts can use the same source port because NAT assigns different external ports.
Port Forwarding:
Allows external access to internal services:
External: 203.0.113.1:443 → Internal: 192.168.1.10:443
Anyone connecting to the external IP on port 443
gets forwarded to the internal web server.
Predictable ephemeral port assignment can enable attacks like DNS cache poisoning (if an attacker can guess the source port of a DNS query, they can forge responses). Modern systems use randomized port selection to mitigate this. This is why sequential port allocation is considered a security weakness.
We've comprehensively explored port identification—the mechanism that enables transport layer multiplexing and demultiplexing. Let's consolidate the key concepts:
What's Next:
Ports enable identification at the transport layer, but they don't tell the whole story. For TCP, connections rather than just ports determine how data is demultiplexed. The next page explores connection identification—how TCP uses the complete 4-tuple to distinguish between thousands of simultaneous connections.
You now understand the complete port identification system. You can explain port ranges and their purposes, describe how ports flow through multiplexing and demultiplexing, recognize common port numbers, and understand the security implications of port usage.