我是靠谱客的博主 个性泥猴桃,这篇文章主要介绍C++:通过gethostbyname函数,根据服务器的域名,获取服务器IP,现在分享给大家,希望可以做个参考。

  本代码的编译环境为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:%sn", ht->h_addrtype == AF_INET ? "AF_INET" : "AF_INET6");
        printf("length:%dn",ht->h_length);
        for(int i=0; ;i++) {
            if(ht->h_addr_list[i]!=NULL) {
                printf("IP Address :%sn",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:%sn",ht->h_aliases[j]);
            }else{
                break;
            }
        }
    }
    return 0;
}

  EOF

转载于:https://www.cnblogs.com/diligenceday/p/6252259.html

最后

以上就是个性泥猴桃最近收集整理的关于C++:通过gethostbyname函数,根据服务器的域名,获取服务器IP的全部内容,更多相关C++:通过gethostbyname函数,根据服务器内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(164)

评论列表共有 0 条评论

立即
投稿
返回
顶部