zoukankan      html  css  js  c++  java
  • 获取主机信息(C++和C#版)

    在C语言中,主机信息是存储在一个hostent结构体中,它的结构如下:

    struct hostent
    {
        char *h_name;
        char **h_aliases;
        int h_addrtype;
        int h_length;
        char **h_addr_list;    
    };

    解释一下这个结构:

    1. char *h_name; 表示的是主机的规范名。例如 http://www.google.com/ 的规范名其实是 http://www.l.google.com/ 。
    2. char **h_aliases; 表示的是主机的别名。 http://www.google.com/ 就是google他自己的别名。有的时候,有的主机可能有好几个别名,这些,其实都是为了易于用户记忆而为自己的网站多取的名字。
    3. int h_addrtype; 表示的是主机ip地址的类型,到底是ipv4(AF_INET),还是ipv6(AF_INET6)
    4. int h_length; 表示的是主机ip地址的长度
    5. char **h_addr_list; 表示的是主机的ip地址,注意,这个是以网络字节序存储的。不能直接用printf带%s参数来打这个东西,需要调用inet_ntop()函数转换。

    下面是一个获取主机信息的例子。

    #include <ace/OS.h>

    int main(int argc, char *argv[])
    {
        //
    获取本机主机名
        char hostname[20];
        ACE_OS::hostname(hostname,20);

        struct hostent *hptr;
        //
    调用gethostbyname()获取主机地址信息
        if( (hptr = ACE_OS::gethostbyname(hostname) ) == NULL )
        {
            printf("gethostbyname error for host:%s\n", hostname);
            return 0; //
    如果调用gethostbyname发生错误,返回
        }

        //
    将主机的规范名打出来
        printf("hostname:%s\n",hptr->h_name);
        
        char **pptr;
        //
    主机可能有多个别名,将所有别名分别打出来
        for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)
            printf(" alias:%s\n",*pptr);
        
        char ip[32];
        //
    根据地址类型,将地址打出来
        switch(hptr->h_addrtype)
        {
        case AF_INET:
        case AF_INET6:
            pptr=hptr->h_addr_list;
            //
    将刚才得到的所有地址都打出来。其中调用了inet_ntop()函数
            for(;*pptr!=NULL;pptr++)
                printf("address:%s\n", ACE_OS::inet_ntop(hptr->h_addrtype, *pptr, ip, sizeof(ip)));
            break;
        default:
            printf("unknown address type\n");
            break;
        }
        return 0;
    }

    C#和C++一样,主机信息存储在IPHostEntry类中,不过它封装得较好,下列C#代码实现了同样的功能,比C++的要精简得多。

    static void Main(string[] args)
    {
        string hostName = Dns.GetHostName(); //
    获取本地主机名
        IPHostEntry hostInfo = Dns.GetHostEntry(hostName); //获取主机消息

        //打印主机名
        Console.WriteLine("host name:\t{0}",hostInfo.HostName);

        //
    打印主机关联的别名
        foreach (string name in hostInfo.Aliases)
        {
            Console.WriteLine("aliases:\t{0}",name);
        }

        //
    打印ip地址消息
        foreach (IPAddress ip in hostInfo.AddressList)
        {
            Console.WriteLine("ip:\t{0}",ip);
        }
    }

     

     

     

  • 相关阅读:
    JavaScript算法系列之-----------------斐波那契数列(JS实现)
    js中Math.max()求取数组中最大值
    JavsScript中比较大小总结---基于sort()方法
    前端技能大挑战-3(修改this指向)
    前端技能大挑战-2(数组去重)
    前端技能大挑战-1(驼峰命名)
    JavaScript算法系列之-----------------链表反转(JS实现)
    JavaScript算法系列之-----------------字符串排列(JS实现)
    JavaScript算法系列之-----------------替换空格(JS实现)
    JavaScript算法系列之-----------------二维数组中的查找(JS实现)
  • 原文地址:https://www.cnblogs.com/TianFang/p/617920.html
Copyright © 2011-2022 走看看