Obviously, the most common use of the word port refers to shipping. Computing, however, also uses the term port. In fact, it has two uses. The first refers to the socket into which a cable can be plugged. Typical examples would include a USB or ethernet port. Technically, an ethernet socket could be thought of as a networking port. That is not the accepted meaning of the word port in networking. A network port is a logical number that identifies which process or network service network traffic is meant for or is sent by.
Why Are Port Numbers Necessary?
When your computer communicates with a web server on the internet, it puts its IP address in the sender field. This lets the server know to which IP address it needs to send the response. When that response returns to your computer, your browser interprets the response and renders the web page.
This works fine in an idealized system with one piece of software making and receiving network traffic. The real world isn’t like that, though. In any computer, there is a range of software making network traffic. To ensure that network traffic is correctly handed to the right program, in some cases, the proper process in a program. One final addressing layer is used the port number.
Imagine having a web browser with two tabs open and an online video game simultaneously. Each browser tab must be able to send and receive network traffic, potentially simultaneously. The online game also needs to both send and receive network traffic. You don’t want your game to have to filter out random web responses, and you don’t want your browser to need to filter your game traffic, either. Both tabs and the game will be assigned at least one network port each so that the intended recipient process can be easily determined.
The same also works for servers. Suppose you have a server that runs both a webserver and an email server. In that case, the port number lets the operating system easily differentiate which network packets are meant for which service.
What Are Port Numbers?
Port numbers take the form of a 16-bit unsigned integer. This means there are sixteen binary bits to make up the numbers, no negative numbers, and all numbers are whole. This allows 65536 possible port numbers ranging from 0 to 65535. There is no physical port system.
The entire addressing scheme, like with IP addresses and MAC addresses, is entirely logical. You could think of the IP and MAC addresses as the address on a letter to get it to your house/computer. In this analogy, the port number would be like the name on the envelope, making sure it gets to the right person/process in your house/computer.
Within the port number space, there is some other structure. Port number 0 is reserved. In TCP traffic, 0 is entirely unused. In UDP traffic, the port number is optional, with a port of 0 meaning “no port.” This works for UDP because it’s a stateless protocol that doesn’t necessarily expect a response. TCP and UDPs ports are separate. One service can bind to one or more ports on one or both protocols. Services that use both typically attach to the same port numbers in TCP and UDP.
Ports 1-1023 are known as “well-known” ports and generally require administrative permissions to bind to. Some well-known ports are 80 for HTTP, 443 for HTTPS, and 53 for DNS. Next, you’ve got the range 1024 – 49151. These are known as the “registered” ports. These are ports regularly used by certain services, but that isn’t common enough or weren’t early enough to get a number below 1023. Ports between 49152 and 65535 are known as ephemeral ports.
The Use of Port Numbers
IANA, the Internet Assigned Numbers Authority, maintains the well-known and registered port lists. Well-known ports only have one service associated with them. Registered ports, however, may have an array of known services that use the port by default.
Generally, nothing actually stops any program from binding to any port number. As mentioned above, port numbers below 1024 tend to require administrative or root permissions to bind to. Still, you can force any service to bind to port 80, for example, even if it’s not a web server. Port numbers in the well-known and registered ranges tend to only be used by network listeners, with ephemeral ports generally used to initiate connections.
A listener is a port that is opened and left open. It listens for messages so it can process them and send a reply. This is almost exclusively the behavior of server-type services such as a web server. The web server would be hosted on ports 80 and 443, while a browser connecting to those ports would locally use an ephemeral port.
Standard port numbers for application listeners mean that it’s easy to know how to actually connect to a service. For example, when you put HTTPS on the front of a URL, your browser assumes by default that it needs to connect to port 443. In some cases, listeners will be set up on alternate ports. This may be because another service or version of the service s already using the standard port. It may also be an attempt at security through obscurity.
A network port is a logical address. The software can bind to a port, so the operating system knows that any traffic sent to that port is meant for that process. Port numbers range from 0 to 65535. Many services are associated with a standard port number. The servers use these ports, while devices connecting to them use random high number ports. This allows easy default connection to services and ensures that connecting to such a service doesn’t prevent you from hosting another service yourself.