localhost is a hostname, and there's a file in your system that tells your pc that localhost resolves to 127.0.0.1 [or ::1: if its ipv6] - which always points back at your system. We'll come to that shortly.
Practically the big problem with using 'just' IPs is they're hard to remember and you can't change them that easily if you actually want to find things later.
A hostname is a friendlier way to name things - but the back end of the internet is still machines. Hostname resolution is pretty much a way to bridge hostnames and ip addresses.
nslookup tells you what resolves where
C:\Users\faile>nslookup localhost
Server: UnKnown
Address: 192.168.2.1
Name: localhost
Addresses: ::1
127.0.0.1
compare this to say nslookup for superuser.com
C:\Users\faile>nslookup superuser.com
Server: UnKnown
Address: 192.168.2.1
Non-authoritative answer:
Name: superuser.com
Addresses: 151.101.1.69
151.101.65.69
151.101.129.69
151.101.193.69
Name service resolution happens in 2 ways.
(The first 2 lines are my router. We'll come to that)
There's a text file somewhere on the system called a hosts file. Think of this as a post it on your phone with phone numbers written on it. It scales terribly, and you don't need all those phone numbers.
In most modern settings, we use DNS servers instead. Essentially these are a way to look up what IP is associated with what server (like a phonebook) that's updated periodically based off a set of 'master' records that are updated.
There's also a list of known ports - and for http that's port 80 and https that's port 443. If the port is not explicitly specified - it will assume that its the default port.
Now to show this a useful tool is netstat - in this case I'm using netstat -ban
which shows Binary names All connections and Numbers (as opposed to names).
There's a lot there but here's the useful part for us
TCP 192.168.2.121:49800 151.101.1.69:443 ESTABLISHED
[vivaldi.exe]
You see 2 IP addresses - My PC (192.168.2.121) makes a connection througn port 49800 to 151.101.1.49 (which as we've determined, is Super User) via port 443.
Why port 443?
Because I'm posting this answer - and am connected to Super User via https
Looking at my browser
You will notice the lack of explicit port.
So basically when not explicitly set your browser needs to be told 2 things
The host - this can be numeric (in theory, I can connect to 151.101.1.69 and reach the server I am getting data from, but there's a deeper rabbithole involving cache servers, load balancers and other such fun stuff) or a hostname. This is essential.
A protocol - for a web browser, there's only a narrow band of possible protocols. If its not explicitly stated, the client will assume the well known port for the service. You can explicitly state a port, since you often cannot host from a default port. Your client will connect using a high, random port however.
At a deeper level, TCP and UDP are not something the end user worries about too much. They're independent ways of sending messages over IP and its typically only important when opening a port. They're good for different things, and that's about it.
Aren't you looking for the answer to: What happens after I type "localhost:12345" in the browser? – Henk Langeveld – 2019-09-12T05:25:46.467