socket之gethostbyname

来源:互联网 发布:足迹软件 编辑:程序博客网 时间:2024/05/22 15:23
#include<stdio.h> //printf#include<string.h> //strcpy#include<sys/socket.h>#include<netdb.h> //hostent#include<arpa/inet.h> int main(int argc , char *argv[]){    // char *hostname = "www.163.com";    char *hostname = "www.njit.edu.cn";    char ip[100];    struct hostent *he;    struct in_addr **addr_list;    int i;             if ( (he = gethostbyname( hostname ) ) == NULL)     {        //gethostbyname failed        herror("gethostbyname");        return 1;    }         //Cast the h_addr_list to in_addr , since h_addr_list also has the ip address in long format only    addr_list = (struct in_addr **) he->h_addr_list;         for(i = 0; addr_list[i] != NULL; i++)     {        //Return the first one;        strcpy(ip , inet_ntoa(*addr_list[i]) );        printf("%s resolved to : %s\n" , hostname , ip);    }         return 0;}

0 0