通過域名獲得主機ip

通過域名獲得主機IP地址:

#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc,char **argv)
{
    struct hostent *hent;
    int i;
    hent = gethostbyname("baidu.com");
    printf("hostname: %s\n address list: \n", hent->h_name);
    for(i = 0; hent->h_addr_list[i]; i++)
    {
        printf("%s\n", inet_ntoa(*(struct in_addr*)(hent->h_addr_list[i])));
    }
    return 0;
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章