实现类似nslookup

版权声明: https://blog.csdn.net/dashoumeixi/article/details/84994979

主要就一个函数 gethostbyname

#include "util.h"
#include <netdb.h>
extern int h_errno;


int main(int argc, char**argv)
{
    if(argc !=2 ){
        puts("plz input www.xxx.com");
        return -1;
    }
    struct hostent* ent = gethostbyname(argv[1]);
    if(!ent){
        perror("gethostbyname");
        return -1;
    }
    printf("hostname:%s\n" , ent->h_name);
    for(int i = 0; ent->h_aliases[i] != NULL; ++i)
        printf("aliases %d :%s\n",i+1, ent->h_aliases[i]);
    printf("type:%s\n" , ent->h_addrtype == AF_INET? "ipv4" : "ipv6");
    for(int i = 0; ent->h_addr_list[i] != NULL ; ++i)
        printf("ip %d : %s\n",i+1,inet_ntoa(*(struct in_addr*)ent->h_addr_list[i]));
    

    return 0;
} 

猜你喜欢

转载自blog.csdn.net/dashoumeixi/article/details/84994979