获取网络主机信息

获取网络主机信息

作者:LAMP小白  点击:1960  发布日期:2012-12-18 22:53:00  返回列表
[root@localhost 201212]# ./10
get the host:www.sina.com addr
name:cernetnews.sina.com.cn
type:AF_INET
length:4
IP:164.33.189.9
alias 0:www.sina.com
alias 1:us.sina.com.cn
alias 2:news.sina.com.cn

alias 3:jupiter.sina.com.cn

#include mio_lt;netdb.hmio_gt;
#include mio_lt;string.hmio_gt;
#include mio_lt;stdio.hmio_gt;
    int main( void )
    {
        char host[] = "www.sina.com";
        struct hostent *ht = NULL;
        ht = gethostbyname( host );
        if( ht )
        {
            int i = 0;
            printf( "get the host:%s addrn", host );
            printf( "name:%sn", ht-mio_gt;h_name );
            printf( "type:%sn", ht-mio_gt;h_addrtype == AF_INET?"AF_INET":"AF_INET6" );
            printf( "length:%dn", ht-mio_gt;h_length );
            for( i=0; ;++i)
            {
                if( ht-mio_gt;h_addr_list[i] != NULL )
                {
                    printf("IP:%sn", inet_ntoa( (unsigned int* )ht-mio_gt;h_addr_list[i] ) );
                }
                else
                {
                    break;
                }
            }
            for( i=0; ;++i)
            {
                if( ht-mio_gt;h_aliases[i] != NULL )
                {
                    printf( "alias %d:%sn", i, ht-mio_gt;h_aliases[i] );
                }
                else
                {
                    break;
                }
            }
            return 0;
        }
    }





上一篇:newwork is unreachable 下一篇:快递查询API
0