获取域名的ip

/* 
 Get the host's multiple IPs.
 Copyright (C) 2006, Li Suke, School of Software,Peking University
 This  is free software; you can redistribute it and/or
 modify it freely.
 
 This software  is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
*/
 
 
#include <cstdlib>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cerrno>
#include <iostream>
 
using namespace std;
 
int main(int argc, char *argv[])
{
   struct hostent *h;
   char **hp;
   const char *p = 0;
 
   char ip_str[INET_ADDRSTRLEN];
 
   int i = 0 ;
 
   if (argc != 2) {
       cout << "Usage:hostiplist <hostname> " << endl;
       exit(1);
   }
 
   h = gethostbyname(argv[1]);
   if (h == 0) {
      herror("gethostbyname");
      exit(1);
   }
  
   hp = h->h_addr_list;
 
   /* We can get the ip address list in this way: */
   for(; *hp!= 0; hp++){
      p = inet_ntop(AF_INET,(*(struct in_addr **)hp),
                    ip_str,INET_ADDRSTRLEN);
      if (p != 0) {
          cout <<"get ip:" << ip_str << endl; 
      }
   }
   /* We also can get the ip address list in another way: */
   for(i=0;h->h_addr_list[i]!=0 ;i++){
      p=inet_ntop(AF_INET,((struct in_addr *)h->h_addr_list[i]),
                           ip_str,INET_ADDRSTRLEN);
      if (p != 0) { 
         cout << "other ip:"<<ip_str << endl;
      }
   }
   cout << "official domain name is "<< h->h_name << endl;
   /* We also can get the alias domain name: */
   for(i=0;h->h_aliases[i]!=0; i++){
      cout << "alias is " <<  h->h_aliases[i] << endl;
   }
   return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值