gethostbyname

#include <netdb.h>

/*返回:若成功则为非空指针,若出错则为NULL且设置h_errno*/
struct hostent *gethostbyname(const char *hostname);

struct hostent {
        char *h_name;           /*official 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*/
};

当发生错误时,它不设置errno变量,而是将全局变量h_errno设置为在头文件<netdb.h>中定义的下列常值之一:

HOST_NOT_FOUND;

TRY_AGAIN;

NO_RECOVERY;

NO_DATA;

gethostbyname执行的是对A记录的查询,它只能返回IPv4地址。

posted @ 2022-09-15 22:59  东宫得臣  阅读(74)  评论(0编辑  收藏  举报