根据域名获取IP

/**************************************************************************************************************************************
 *
 *   file name:GetHostByName.c
 *   author   :momolyl@126.com
 *   date     :2024/06/04
 *   brief    :设计程序实现解析www.baidu.com的域名,把获取到的百度的IP地址全部输出到终端并验证是否正确。
 *   note     :   ./xxx www.baidu.com
 *
 *   CopyRight (c) 2024    momolyl@126.com    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;
}

运行结果
image

posted @ 2024-06-04 20:13  铃是铃铛的铃  阅读(29)  评论(0)    收藏  举报