首页 > 其他分享 >Analysis the domain name of Baidu

Analysis the domain name of Baidu

时间:2024-06-04 21:57:28浏览次数:16  
标签:Baidu addr domain name ip struct include myHW

/*********************************************************************************************************
 *  file name:   hw.c
 *  author   :   [email protected]
 *  data     :   2024/06/04
 *  function :   设计程序实现解析www.baidu.com的域名,把获取到的百度的IP地址全部输出到终端并验证是否正确。
 *  note     :   None
 *
 *
 *  CopyRight (c) 2023-2024     All Right Reseverd
 *
 * ******************************************************************************************************/

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/udp.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <netdb.h>
#include <sys/socket.h>

int main()
{
  // gethostbyname句柄类型为struct hostent *
  struct hostent *myHW;

  // 获取百度的ip地址
  myHW = gethostbyname("www.baidu.com");
  /*struct hostent *gethostbyname(const char *name);*/

  if (myHW)
  {
    int i = 0; // ip地址的下标

    // myHW->h_addr_list[i]循环直至NULL,退出循环
    while (myHW->h_addr_list[i])
    {
      // 获取ip转化为网络字节序,网络字节序再转化为ip地址
      struct in_addr *addr = (struct in_addr *)myHW->h_addr_list[i];

      printf("baiduIP is : %s\n", inet_ntoa(*addr));

      i++;
    }
  }
  else
  {
    perror("Unable analysis domain name\n");
  }

  return 0;
}

编译运行
image

标签:Baidu,addr,domain,name,ip,struct,include,myHW
From: https://www.cnblogs.com/hhail08/p/18231825

相关文章