Hostname vs. IP address

I am currently implementing openssl in my application. My problem came when I had to set the hostname, IP address and BIO port. I've always known that ip and hostname are the same thing. Can someone explain this difference.

+3


source to share


2 answers


The hostname is a combination of your computer name and domain name (for example, machinename.domain.com). The purpose of a hostname - readability - is much easier to remember than an IP address. All host names resolve to IP addresses, so in many cases they say they are interchangeable.



+7


source


A hostname can have multiple IP addresses, but not vice versa. If you look

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

you will see that gethostbyname () returns a list of addresses for a specific 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;
}

      

By entering www.yahoo.com I get this:

98.137.246.8
98.137.246.7
98.138.219.232
98.138.219.231

0


source







All Articles