Hostname vs. IP - address

自作多情 提交于 2019-12-21 12:14:43

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/26540583/hostname-vs-ip-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!