I'm starting to learn sockets, and I'm trying to figure out why you need to specify the hostname in the request. If I am already connected to "www.google.com", then what is the point of "Host: www.google.com\r\n" in the request? Doesn't the server already know its own name?
The hostname is used to distinguish devices within a local network. In addition, computers can be found by others through the hostname, which enables data exchange within a network, for example. Hostnames are used on the internet as part of the fully qualified domain name.
HTTP 1.1 requests often include a Host: header, which contains the hostname from the client request. This is because a server may use a single IP address or interface to accept requests for multiple DNS hostnames. The Host: header identifies the server requested by the client.
host name just means the name of the host. server is also a host ...... Not necessarily the same. Multiple servers (in the sense of binaries that respond to network communication, e.g. a web server, a mail server) can run on the same host (in the sense of a physical or virtual machine).
The first character must be an alphabetic character and the last character must not be a minus sign or period. It was also recommended that the host name be no longer than 24 characters in length. Subsequently, in “Requirements for Internet Hosts – Application and Support“, the host name rules were updated.
Sometimes the server does know its hostname, but it isn't always a 1:1 relationship. Many DNS hostnames can all point to the same IP, thus, 1 IP can have many DNS hostnames. (Also, a single DNS hostname can map to many IP addresses, but this is less important in this question.)
The Host:
line in an HTTP request allows a web server to know which hostname you requested, and serve based on that. This allows one machine at an IP address to serve many domains.
For example, if a webserver sees Host: foo.com
, it might serve one website, but Host: bar.com
might result in a completely different result being returned. There is no other piece of data available to the webserver with this information, and it relies on the web client to inform it.
A given server can have many different DNS names. Think shared hosting sites for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With