zoukankan      html  css  js  c++  java
  • 域名解析

    在/etc/hosts中已经设置了ip与域名的映射关系,通过域名解析函数进行解析(主机名,别名,协议类型,网络地址大小,指向网络IP地址指针等)

    部分解析函数:

    #include<netdb.h>

    struct hostent gethostbyname(const char *hostname);  无法解析IPV6地址,在多线程中会有问题

    struct hostent gethostend(void);

    ----------------------------

    #include <netdb.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <memory.h>

    void out_addr(struct hostent *h)
    {
        printf("hostname: %s ",h->h_name);
        printf("addrtype: %s ",
                h->h_addrtype == AF_INET? "IPV4":"IPV6");
        char ip[16];
        memset(ip,0,sizeof(ip));
        inet_ntop(h->h_addrtype,h->h_addr_list[0],ip,sizeof(ip));
        printf("ip address:%s ",ip);
        
        int i = 0;
        while(h->h_aliases[i] != NULL)
        {
            printf("aliase: %s ",h->h_aliases[i]);
        }
    }

    int main(int argc,char *argv[])
    {
            if(argc < 2)
            {
                fprintf(stderr,"usage: %s host ",argv[0]);
                exit(1);
            }
            struct hostent *h;
            while((h=gethostent)!= NULL)
            {
                if(!strcmp(argv[1],h->h_name))
                {
                    out_addr(h);
                    exit(0);
                }
                else
                {
                    int i=0;
                    while(h->h_aliases[i] != NULL)
                    {
                        if(!strcmp(argv[1],h->h_aliases[i]))
                        {
                            out_addr(h);
                            exit(0);
                        }
                        i++;
                    }
                }
            }
            endhostent();
            printf("no %s exist ",argv[1]);
            
            return 0;
    }






  • 相关阅读:
    Cxx11 stdchrono库详解
    Oracle中文乱码
    Javascript 编程小技巧总结(部分内容借鉴他人)
    从文档流角度理解浏览器页面渲染引擎对元素定位的解析
    JS重点特性——闭包详解
    用一段JS代码来比较各浏览器的极限内存与运算速度
    前端开发人员需知——浏览器详解
    Js变量定义——fn里 var与不var的区别
    Js文字特效—文字段逐个变色循环
    html5 canvas画图之图形随拖动而复制(有操作指示)
  • 原文地址:https://www.cnblogs.com/lvdh1314/p/6506557.html
Copyright © 2011-2022 走看看