What is a port
By Flavio Copes
Learn what a port is in networking, how it lets many apps share one machine, and the default ports like 80 for HTTP and 443 for HTTPS out of 65535 total.
A port is a number that identifies a specific application on a machine. The IP address gets your data to the right computer, and the port gets it to the right program running on that computer.
When making network requests, you use an IP address, or a host name, and a port.
Like this:
http://localhost:8080(port 8080)ftp://127.0.0.1:29392(port 29392)
Why do ports exist?
It’s a technique introduced to allow multiple applications to respond on the same computer, on the same protocol.
Think about your own machine while you develop. You might have a web server, a database, and an API server all running at the same time. They share one IP address, so the operating system uses the port to know which program should receive each incoming connection.
For example we might have a web server running on our machine. A second web server can be started on a different port.
Say the first runs on port 80, which is the default for web servers using the HTTP protocol. The same can run on any other port except port 80. It’s common to use port 8080 or port 81, for example.
HTTPS runs on port 443 by default.
Every protocol has a different default port, but programs are not required to use that. They can use any unused port between 1 and 65535 (16 bits unsigned = 2^16). Here is a list of default port numbers for all protocols based on TCP and UDP.
Ports below 1024 are called well-known ports, and on Unix-like systems a program needs root privileges to listen on them. That’s why development servers default to high ports like 3000 or 8080: any user can bind them.
The client side of a connection uses a port too. When your browser connects to a website on port 443, the operating system assigns a random high port to the browser’s side. That’s how one machine can keep thousands of connections open at once.
Some historical default ports
Historically:
- FTP uses 21
- Telnet uses 23
- SMTP uses 25
- POP3 uses 110
..although now for all those services you should use secure counterparts, which are based on other ports.
When a port is already taken
Only one program can listen on a port at a time. If you start a server on a port that’s busy, you get an error, in Node.js it’s EADDRINUSE: address already in use.
This usually means an old instance of your server is still running. On macOS or Linux you can find it with:
lsof -i :3000
The output shows the process using port 3000 and its PID. Stop that process, or start your server on a different port, and the error goes away.
Related posts about network: