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;
}