Ports
What is a Port?
A port is a logical communication endpoint on a device.
Think of it as a door through which a particular application sends or receives network traffic.
- IP Address → Identifies the device
- Port Number → Identifies the application/service on that device
Without ports, the operating system wouldn't know which application should receive incoming data.
Why do we need Ports?
Imagine a server running:
- Website
- Database
- SSH
- Email server
All of them share the same IP address.
IP:
192.168.1.10
How does the OS know where incoming packets belong?
192.168.1.10:80
→ Website
192.168.1.10:22
→ SSH
192.168.1.10:5432
→ PostgreSQL
The port number tells the operating system which application should receive the packet.
IP vs Port
| IP Address | Port |
| Identifies a device | Identifies a process/application |
| Network level | Transport level (TCP/UDP) |
| Example: 192.168.1.5 | Example: 443 |
Together they form:
192.168.1.5:443
Analogy
Imagine an apartment building.
Building Address
↓
221B Baker Street
Inside the building are many apartments.
Apartment 101
Apartment 102
Apartment 103
Here,
- Building Address = IP Address
- Apartment Number = Port
The postman reaches the building using the address.
The apartment number tells him exactly where to deliver the package.
Port Number Range
Ports are 16-bit numbers.
0 - 65535
Because
2^16 = 65536
Port Categories
1. Well-known Ports
0 - 1023
Reserved for standard protocols.
Examples
| Port | Service |
| 20 | FTP Data |
| 21 | FTP Control |
| 22 | SSH |
| 23 | Telnet |
| 25 | SMTP |
| 53 | DNS |
| 80 | HTTP |
| 110 | POP3 |
| 143 | IMAP |
| 443 | HTTPS |
These are standard ports recognised worldwide.
2. Registered Ports
1024 - 49151
Used by applications.
Examples
| Port | Service |
| 3306 | MySQL |
| 5432 | PostgreSQL |
| 6379 | Redis |
| 5672 | RabbitMQ |
| 8080 | Alternative HTTP |
| 27017 | MongoDB |
49152 - 65535
Used temporarily by clients.
Example
Browser
Your Laptop
192.168.1.20:52341
│
│
▼
Google Server
142.250.xxx.xxx:443
Here,
52341
is automatically chosen by the operating system.
After the connection closes, this port is released.
What is a Socket?
A socket uniquely identifies one network connection.
A socket consists of:
IP Address
+
Port
+
Protocol (TCP/UDP)
Example
192.168.1.20:52341
↓
connects to
↓
142.250.xxx.xxx:443
The full connection is identified by the 5-tuple:
Source IP
Destination IP
Source Port
Destination Port
Protocol
Example:
Source IP: 192.168.1.20
Destination IP: 142.250.1.5
Source Port: 52341
Destination Port:443
Protocol: TCP
This uniquely identifies the connection.
Can Two Applications Use the Same Port?
No.
Only one process can bind to a specific IP + port + protocol combination at a time (with some specialised exceptions such as socket reuse).
Example
Node.js
Port 3000
Trying to start another server on the same port gives:
Error:
EADDRINUSE
(Address already in use)
Can One Application Use Multiple Ports?
Yes.
Example:
Nginx
80
443
Both belong to the same application.
Why does HTTPS use Port 443?
It's simply the standard port assigned to HTTPS.
Similarly,
HTTP → 80
SSH → 22
DNS → 53
The protocol itself doesn't require those exact numbers, but using the standard ports avoids having to specify custom ones.
Why do we write localhost:3000?
localhost
resolves to
127.0.0.1
The server is listening on
Port 3000
So
http://localhost:3000
means
127.0.0.1:3000
What happens when you visit https://google.com?
Browser
│
▼
DNS resolves google.com
│
▼
Gets Google's IP
│
▼
Browser opens TCP connection
Destination Port = 443
│
▼
TLS Handshake
│
▼
HTTPS communication begins
Client Port vs Server Port
Suppose you visit:
https://example.com
Connection:
Your PC
192.168.1.15:54821
│
│
▼
Server
203.0.113.10:443
Server:
443
Client:
54821
The client port is chosen automatically by the operating system.
Are Ports Part of TCP/IP?
Ports belong to the Transport Layer protocols such as TCP and UDP.
The IP header contains only IP addresses.
The TCP/UDP header contains:
- Source Port
- Destination Port
That's why ports are considered a Transport Layer concept, not a Network Layer concept.
Common Ports to Remember
| Port | Protocol / Service |
| 20 | FTP Data |
| 21 | FTP Control |
| 22 | SSH |
| 23 | Telnet (legacy, insecure) |
| 25 | SMTP |
| 53 | DNS (TCP/UDP) |
| 80 | HTTP |
| 110 | POP3 |
| 143 | IMAP |
| 443 | HTTPS |
| 3306 | MySQL |
| 5432 | PostgreSQL |
| 6379 | Redis |
| 8080 | Alternative HTTP / Development |
| 27017 | MongoDB |
Ports allow multiple networked applications to run simultaneously on the same machine.
3. What is the difference between an IP address and a port?
An IP address identifies where to send data (the device), whereas a port identifies which application on that device should receive it.
4. Why can't two servers use the same port?
Because the operating system can only bind one process to the same IP + port + protocol combination at a time.
Otherwise, it wouldn't know which process should receive incoming packets.
5. What is an ephemeral port?
An ephemeral port is a temporary client-side port automatically assigned by the operating system for the duration of a connection.
It is released after the connection ends.
6. Why is HTTP on port 80 and HTTPS on port 443?
These are standard, well-known ports assigned by convention.
The protocols can technically run on any port, but using the standard ports ensures interoperability and avoids needing to specify custom ports.
7. Can TCP and UDP use the same port number?
Yes.