linux下用域名解析ip地址列表

linux下用域名解析ip地址列表

头文件/宏定义

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

主函数

/********************************************************************
 *
 *	name	 :  main
 *	function :  linux下用c语言利用域名解析ip地址列表
 *	argument :
 *				@n  :argc
 *              @n  :argv
 *	retval	 :  none
 *	author	 :  17647576169@163.com
 *	date	 :  2024年6月4日
 * 	note	 :
 *
 * *****************************************************************/
int main(int argc, char *argv[])
{
    struct hostent *he;

    he = gethostbyname("www.baidu.com");
    // 域名无效
    if (he == NULL)
    {
        herror("gethostbyname");
        exit(1);
    }
    printf("Host name: %s\n", he->h_name);
    int i = 0;
    while (((struct in_addr **)he->h_addr_list)[i])
    {
        printf("IP Address[%d]: %s\n ", i, inet_ntoa(*((struct in_addr **)he->h_addr_list)[i]));
        i++;
    }

    printf("\n");

    return 0;
}

验证结果

image

posted @ 2024-06-04 19:55  谁TM买小米啊  阅读(4)  评论(0编辑  收藏  举报