How does browser decide which destination port to use

layer4transport-protocol

Suppose I have a Vanilla machine and this is the first time I am connecting it to Internet. There is no Cache at any level. I access a website through browser by typing example.com (no http//: or https//:). The first thing that will happen is that machine will resolve the domain name to IP and then a TCP Handshake will take place. There is no port negotiation that has happened yet then how does the browser knows which port to connect to?

I have gone through some articles on the web which states that HSTS will tell the browser which port to use as per the HTTP directive. But this does not explains how the browser decides on the port that it needs to connect to initially? How does the machine's OS knows which traffic is this (ICMP or HTTP). Is it always going to connect on port 80 or some other port first and then the sever would be able to tell which port it will listen on?

Best Answer

There are registered, well-known port numbers for services and transport protocols that use port numbers as transport addresses.

But this does not explains how the browser decides on the port that it needs to connect to initially?

HTTP, by default, uses TCP at the registered, well-known port number 80, and HTTPS, by default, uses TCP at the registered, well-known port number 443. The browser defaults to the default transport protocol and registered, well-known port number for the browser protocol in use. If the server is listening at a different port number, this can be overridden in the URI by appending :<port number> to the FQDN. For example, http://www.example.com:12345.

How does the machine's OS knows which traffic is this (ICMP or HTTP).

The protocol is at the beginning of the URI, e.g. http://, https://, ftp://, etc.

Is it always going to connect on port 80 or some other port first and then the sever would be able to tell which port it will listen on?

The browser will connect (or not if the server is not listening at what the browser tries) based on the URI.


The registered port numbers are maintained by IANA in the Service Name and Transport Protocol Port Number Registry.