Why do we need to specify a port number while using HTTP protocol?
The default port for HTTP is 80 and HTTPS is 443 but port numbers range from 0 to 65535. Most web servers listen on the default port so it's simpler to refer to the URL without the port. For example, https://www.cnn.com/
refers to the same resource as https://www.cnn.com:443/
since port 443 is the default port.
If an HTTP/HTTPS service is other than the default port then the port must be specified in the URL.
The URL is defined by RFC 1738 with this simplified syntax which includes the port:
<scheme>://<host>:<port>/<url-path>
So for example, the URL http://xyz:8080/...
refers to a host machine name or IP address listening to port 8080 with HTTP as the protocol. The HTTP (or web) server at that address then resolves the url-path to the particular service or file.
The port is optional when it is the default port for the given scheme or protocol (e.g., HTTP=80).
A given machine can host multiple different products with HTTP services on different ports. For example, Apache web server is listening to port 80 on a given server while Apache Tomcat is listening to port 8080 on the same machine. Various database and messaging products typically have HTTP services on different ports.
The port is part of the address from which a client can reference a specific service.
A port is like a "channel" in a way... If you have to access different functions of the same website, you use different ports. HTTP is port 80, HTTPS is 443, SSH is 22, and so on.