zl程序教程

您现在的位置是:首页 >  系统

当前栏目

Linux通过域名得到ip

Linux域名IP 通过 得到
2023-09-14 09:09:56 时间
#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char *argv[]){
  if (argc < 2)
    return -1;

  struct hostent *p = gethostbyname(argv[1]);
  printf("hostname %s\n", p->h_name);
  printf("address ");

  int i;
  for (i = 0; p->h_addr_list[i]; i++) {
    //inet_ntoa: 将网络地址转换成“.”点隔的字符串格(点分十进制)
    printf("%s ", inet_ntoa(*(struct in_addr *)p->h_addr_list[i]));
  }
  printf("\n");

  return 0;
}