Port
In short
A port is a number from 0 to 65535 that identifies a specific program or service on a device, so traffic reaching an IP address gets to the right application.
What is a port in networking?
In networking, a port is a number that identifies a specific program or service running on a computer. An IP address gets data to the right machine, and the port number gets it to the right application on that machine. Port numbers range from 0 to 65535 and are used by both TCP and UDP.
When a server program starts, it listens on a port, such as a web server waiting on port 443. A client connecting to it uses a temporary port of its own, called an ephemeral port, so each connection is identified by the combination of both IP addresses and both ports. Ports 0 to 1023 are well-known ports reserved for common services, such as 22 for SSH, 53 for DNS, 80 for HTTP, and 443 for HTTPS.
Think of an IP address as the street address of an apartment building and the port as the apartment number. The mail carrier gets the letter to the building, and the apartment number decides who receives it. Developers meet ports constantly, for example when a local development server runs at localhost:3000 or when a firewall rule allows only port 443.
A network port is not the same as a physical port, such as a USB or Ethernet socket on a device; a network port is purely a number in software. The same word is also used for adapting software to run on another platform, which is unrelated. Only one program can listen on a given port and address at a time, which is why starting a second one fails with errors like EADDRINUSE (address already in use).
Key takeaways
- A port number identifies a specific program or service on a device.
- Port numbers range from 0 to 65535, and TCP and UDP each have their own set.
- Well-known ports include
22(SSH),53(DNS),80(HTTP), and443(HTTPS). - A connection is identified by source IP, source port, destination IP, and destination port.
- Only one program can listen on the same port and address at a time.
Example
# Start a simple web server listening on port 8000
python3 -m http.server 8000
# In another terminal, send a request to that port
curl http://localhost:8000/
# List programs listening on TCP ports (Linux)
ss -tlnpReaders ask
What is the difference between an IP address and a port?
An IP address identifies a device on a network, while a port identifies a specific program or service on that device. Together, as in 192.168.1.10:443, they point to one exact destination.
What does localhost:3000 mean?
localhost refers to your own computer, and 3000 is the port where a program, often a development server, is listening. Opening that address in a browser connects to that program on your machine.
What does it mean when a port is open?
An open port has a program listening on it, and the firewall allows traffic to reach it. Closing unused ports reduces the number of ways an attacker can reach a machine.
See also
- IP AddressNetworking, p. 10An IP address is a numeric label assigned to each device on a network so that data can be routed to it, much like a postal address for a house.
- TCPNetworking, p. 30TCP is a core internet protocol that delivers data between two programs reliably and in order, by opening a connection and resending anything that gets lost.
- UDPNetworking, p. 35UDP is a lightweight internet protocol that sends small, independent messages called datagrams without a connection, favoring speed over guaranteed delivery.
- FirewallNetworking, p. 8A firewall is a security system that checks network traffic and allows or blocks it based on rules, acting as a barrier between trusted and untrusted networks.
- HTTPWeb Development, p. 19HTTP is the protocol that browsers, apps, and servers use to exchange web pages and data through a simple cycle of requests and responses.
- Load BalancerDevOps & Cloud, p. 34A load balancer is a server or service that spreads incoming traffic across several backend servers so no single one is overloaded and the app stays available.
Spotted a mistake or something missing on this page?Suggest an edit