zoukankan      html  css  js  c++  java
  • gethostbyname

    SYNOPSIS

    #include <netdb.h>
    
    struct hostent *gethostbyname(const char *name);

    Data Structure

    http://www.cnblogs.com/LubinLew/p/POSIX-DataStructure.html#struct_hostent 

    Description

    gethostbyname 是不可重入函数,在多线程编程时需要注意, linux中有可重入版本 gethostbyname_r,

    POSIX标准里面使用getaddrinfo和getnameinfo来替换gethostbyname系列函数了,这些函数已经已经被废弃了。 

    Parameter

    参数 name 既可以是一个主机名(例如"www.163.com"),也可以是IPv4地址(点分十进制形式,例如"111.202.60.48")或者IPv6地址(冒号分隔16进制地址.例如"2000::1:2345:6789:abcd").

    如果参数是IPv4或者IPv6地址,那么函数不会执行查询操作,返回的结构体中 h_name 就是该参数的拷贝, h_addr_list[0]就是该参数对应的的整型网络字节(相当于inet_pton(AF_INET*, name, dst)).

     If name doesn’t end in a dot and the environment variable HOSTALIASES is set, the alias file pointed to by HOSTALIASES will first be searched for name (see hostname(7) for the file format).

    The current domain and its parents are searched unless name ends in a dot.

    Example

    #include <stdio.h>
    #include <netdb.h>
    #include <arpa/inet.h>
    
    int main(void)
    {
        int i = 0;
        char str[32] = {0};
         struct hostent* phost = NULL;
     
        phost = gethostbyname("www.163.com");
        if (NULL == phost)
        {
            return -1;    
        }
        
        printf("---Offical name:
    	%s
    ", phost->h_name);
    
        printf("---Alias name:
    ");    
        for (i = 0;  phost->h_aliases[i]; i++)
        {
            printf("	%s
    ", phost->h_aliases[i]);
        }
    
        printf("---Address list:
    ");
        for (i = 0; phost->h_addr_list[i]; i++)
        {
            printf("	%s
    ", inet_ntop(phost->h_addrtype,  phost->h_addr_list[i], str, sizeof(str)-1));
        }
    
        return 0;
    } 

    运行结果

    ---Offical name:
    	163.xdwscache.glb0.lxdns.com
    ---Alias name:
    	www.163.com
    	www.163.com.lxdns.com
    ---Address list:
    	111.202.60.48
    	111.202.60.47
  • 相关阅读:
    poj1966 Cable TV Network
    contesthunter#17-c 舞动的夜晚
    joyoi1957 「Poetize5」Vani和Cl2捉迷藏
    joyoi1935 「Poetize3」导弹防御塔
    luogu3629 [APIO2010]巡逻
    poj2728 Desert King
    poj1734 Sightseeing trip
    loj2003 「SDOI2017」新生舞会
    hdu2255 奔小康赚大钱 KM 算法
    POJ 1681 Painter's Problem(高斯消元+枚举自由变元)
  • 原文地址:https://www.cnblogs.com/LubinLew/p/Linux-gethostbyname.html
Copyright © 2011-2022 走看看