• Unix Network Programming Episode 76


    We encourage the use of getaddrinfo (Section 11.6(See 8.9.6)) in new programs.

    #include 
    struct hostent *gethostbyname (const char *hostname);
    
    • 1
    • 2

    The non-null pointer returned by this function points to the following hostent structure:

    struct hostent {
    	char *h_name; /* official (canonical) name of host */
    	char **h_aliases; /* pointer to array of pointers to alias names */
    	int h_addrtype; /* host address type: AF_INET */
    	int h_length; /* length of address: 4 */
    	char **h_addr_list; /* ptr to array of ptrs with IPv4 addrs */
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    gethostbyname differs from the other socket functions that we have described in that it does not set errno when an error occurs. Instead, it sets the global integer h_errno to one of the following constants defined by including :

    • HOST_NOT_FOUND
    • TRY_AGAIN
    • NO_RECOVERY
    • NO_DATA (identical to NO_ADDRESS)

    The NO_DATA error means the specified name is valid, but it does not have an A record. An example of this is a hostname with only an MX record.
    Most modern resolvers provide the function hstrerror, which takes an h_errno value as its only argument and returns a const char * pointer to a description of the error.

    #include "unp.h"
    
    int main(int argc, char **argv)
    {
        char *ptr, **pptr;
        char str[INET_ADDRSTRLEN];
        struct hostent *hptr;
    
        while(--argc>0)
        {
            ptr=*++argv;
            if((hptr=gethostbyname(ptr)==NULL)
            {
                err_msg("gethostbyname error for host: %s: %s",ptr, hstrerror,(h_errno));
            }
            continue;
        }
        printf("official hostname: %s\n", hptr->h_name);
    
        for(pptr=hptr->h_aliases;*pptr!=NULL;pptr++)
            printf("\talias:%s\n", *pptr);
        
        switch(hptr->h_addrtype)
        {
            case AF_INET:
                pptr=hptr->h_addr_list;
                for(;*pptr!=NULL;pptr)
                {
                    printf("\taddress: %s\n",Inet_ntop(hptr->h_addrtype,*pptr, str, sizeof(str)));
                }
                break;
            default:
                err_ret("unknown address type");
                break;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    Call gethostbyname and print returned information

  • 相关阅读:
    未来MCU设计的六个方向
    基于Springboot实现高校社团管理系统
    【iOS】Category、Extension和关联对象
    JAVA获取30天或某段范围日期的方法
    实现延迟队列的几种途径
    CP03大语言模型ChatGLM3-6B特性代码解读(1)
    毫米波成像 论文阅读笔记 | HawkEye, CVPR 2020
    阿里云面试:什么是语法糖?Java中有哪些语法糖?
    【scikit-learn基础】--『监督学习』之 层次聚类
    C++Day5
  • 原文地址:https://blog.csdn.net/myfather103/article/details/82586108