zoukankan      html  css  js  c++  java
  • 检索并打印一个DNS主机条目

    检索并打印一个DNS主机条目的 C 程序 --- Linux/Unix

    /*************************************************************************
    * File Name: hostinfo.c
    * Author: Chen WenKe
    * Email: chenwenke666@gmail.com
    * Blog: https://caotanxiaoke.github.io
    * Created Time: Tue 13 Jun 2017 11:30:34 PM PDT
    *
    * Description: 
        检索并打印一个 DNS 主机条目。     
     ************************************************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <arpa/inet.h>
    #include <netdb.h>  // for gethostbyname() / gethostbyaddr()
    
    int main(int argc, char **argv)
    {
        char **pp;
        struct in_addr addr; 
        struct hostent *hostp; 
    
        if (argc != 2)
        {
            fprintf(stderr, "usage: %s <domain name or dotted-decimal>
    ", argv[0]);
            exit(0);
        }
    
        if (inet_aton(argv[1], &addr) != 0)
            hostp = gethostbyaddr((const char *)&addr, sizeof(addr), AF_INET);
        else
            hostp = gethostbyname(argv[1]); 
    
        printf("official hostname: %s
    ", hostp->h_name); 
    
        for (pp = hostp->h_aliases; *pp != NULL; pp++)
        {
            printf("alias: %s
    ", *pp); 
        }
    
        for (pp = hostp->h_addr_list; *pp != NULL; pp++)
        {
            addr.s_addr = ((struct in_addr *)*pp)->s_addr; 
            printf("address: %s
    ", inet_ntoa(addr)); 
        }
    
        exit(0); 
    }
    
    

    编译:
    gcc -o hostinfo hostinfo.c

    测试:
    ./hostinfo baidu.com



  • 相关阅读:
    java多线程学习-同步之线程通信
    java多线程学习-同步(synchronized)
    java多线程学习-开篇
    面向对象-多线程-异常机制-查漏补缺
    Sprin2.5+Hibernate3.3+Struts2.0.11集成
    Strut1.3+Sprin2.5+Hibernate3.3集成
    Sprin2.5+Hibernate3.3集成
    Spring学习笔记
    Hibernate学习笔记
    Sping
  • 原文地址:https://www.cnblogs.com/acm1314/p/7009182.html
Copyright © 2011-2022 走看看