以下是一个使用C语言通过gethostbyname函数获得给定域名的多个IP地址的示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>

int main() {
    char domain[100];
    printf('请输入域名:');
    scanf('%s', domain);

    struct hostent *host;
    host = gethostbyname(domain);

    if (host == NULL) {
        printf('获取IP地址失败\n');
        return 1;
    }

    printf('域名:%s\n', domain);
    printf('IP地址列表:\n');
    
    int i = 0;
    while (host->h_addr_list[i] != NULL) {
        struct in_addr *addr = (struct in_addr *)host->h_addr_list[i];
        printf('%s\n', inet_ntoa(*addr));
        i++;
    }

    return 0;
}

通过这段代码,用户可以输入一个域名,然后程序会通过gethostbyname函数获取该域名对应的IP地址列表,并打印出来。请注意,gethostbyname函数已经在POSIX.1-2001中废弃,推荐使用getaddrinfo函数来代替。

C语言获取域名多个IP地址:gethostbyname函数示例

原文地址: https://www.cveoy.top/t/topic/wzI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录