文章出處

  本代碼的編譯環境為MAC,系統版本為10.11.6:

#include <string.h>
#include <netdb.h>
#include <stdio.h>
#include <arpa/inet.h>
int main(int argc, char *argv[]) {
    char host[] = "www.baidu.com";
    struct  hostent *ht = NULL;
    ht = gethostbyname(host);
    if(ht) {
        printf("type:%s\n", ht->h_addrtype == AF_INET ? "AF_INET" : "AF_INET6");
        printf("length:%d\n",ht->h_length);
        for(int i=0; ;i++) {
            if(ht->h_addr_list[i]!=NULL) {
                printf("IP Address :%s\n",inet_ntoa(*((struct in_addr *)ht->h_addr_list[i])));
            }else{
                break;
            }
        }
        for(int j=0; ;j++) {
            if(ht->h_aliases[j]!=NULL) {
                printf("IP:%s\n",ht->h_aliases[j]);
            }else{
                break;
            }
        }
    }
    return 0;
}

  EOF


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()