/**************************************************************************************************************************************
*
* file name:GetHostByName.c
* author :[email protected]
* date :2024/06/04
* brief :设计程序实现解析www.baidu.com的域名,把获取到的百度的IP地址全部输出到终端并验证是否正确。
* note : ./xxx www.baidu.com
*
* CopyRight (c) 2024 [email protected] All Right Reseverd
* ***********************************************************************************************************************************/
#include <netdb.h>
#include <stddef.h>
#include <arpa/inet.h>
#include <stdio.h>
extern int h_errno;
int main(int argc, char const *argv[])
{
struct hostent *p = gethostbyname(argv[1]);
char buf[128] = {0};
char **pr = p->h_addr_list;
for (pr = p->h_addr_list; *pr != NULL; pr++)
{
inet_ntop(p->h_addrtype, *pr, buf, sizeof(buf));
printf("The addr get by name is: %s\n", buf);
}
return 0;
}
运行结果