17

I am currently implementing openssl into my application. My problem arose when I had to set the hostname, IP - address, and port of the BIO. I have always known ip and hostname to be the same thing. Could someone please explain the difference.

2 Answers2

29

A host name is a combination of the name of your machine and a domain name (e.g. machinename.domain.com). The purpose of a host name is readability - it's much easier to remember than an IP address. All hostnames resolve to IP addresses, so in many instances they are talked about like they are interchangeable.

Circadian
  • 559
  • 4
  • 4
  • 10
    Just wanted to add that a host name resolves to an IP address by DNS. Not fully relevant to the question but kind of brings this answer full circle. – trnelson Oct 24 '14 at 02:44
  • You saying that a hostname resolves to an IP makes me think that they are the same again –  Oct 24 '14 at 02:45
  • 14
    Say your name is Steve and your phone number is 555-1212. DNS is the phonebook. I can "call steve" or I can "call 555-1212" and get the same result. One's a name, one's an address. – Jim Davis Oct 24 '14 at 02:53
  • 4
    They are not the same: The hostname is a mapping to the IP address. Over time the same hostname could map to a different IP address. This might be done where a web service is rebuilt on a different machine for example. In Jim's example, Steve might change his phone number but he would still be Steve. – Paul Whipp Jul 13 '20 at 03:06
-1

A host name can have multiple IP addresses, but not the other way around. If you check out

https://beej.us/guide/bgnet/html/multi/gethostbynameman.html

you'll see that gethostbyname() returns a list of addresses for a particular host. To prove it, here's a small program:

#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>

int main(int argc, char** argv)
{
    if (argc < 2)
    {
        printf("usage: %s hostname\n", argv[0]);
        return 0;
    }

    struct in_addr addr;
    struct hostent* he = gethostbyname(argv[1]);

    if (!he)
    {
        perror("gethostbyname");
        return 1;
    }

    printf("IP addresses for %s:\n\n", he->h_name);

    for (int i = 0; he->h_addr_list[i]; i++)
    {
        memcpy(&addr, he->h_addr_list[i], sizeof(struct in_addr));
        printf("%s\n", inet_ntoa(addr));
    }

    return 0;
}

Entering www.yahoo.com, I get the following:

98.137.246.8
98.137.246.7
98.138.219.232
98.138.219.231

  • 1
    Downvote for "A host name can have multiple IP addresses, but not the other way around". It's possible, see https://serverfault.com/questions/65781/multiple-reverse-dns-entries. – ChristophK Oct 26 '19 at 09:45