Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5689694
  • 博文数量: 675
  • 博客积分: 20301
  • 博客等级: 上将
  • 技术积分: 7671
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-31 16:15
文章分类

全部博文(675)

文章存档

2012年(1)

2011年(20)

2010年(14)

2009年(63)

2008年(118)

2007年(141)

2006年(318)

分类: C/C++

2006-10-04 21:07:21

查看主机信息
#include
#include
#include
#include

int main(int argc,char **argv)
{
    struct hostent * hostp;
    struct in_addr addr;
    char **pp;

    if (argc != 2) {
        fprintf(stderr, "usage: %s \n", argv[0]);
        exit(0);
    }
    
    if(inet_aton(argv[1],&addr) != 0)
    {
        hostp = gethostbyaddr((const char *)&addr,sizeof(addr),0);
    }
    else
        hostp = gethostbyname(argv[1]);
    
    printf("Name: %s\n",hostp->h_name);
    for(pp=hostp->h_aliases;*pp!=NULL;pp++)
        printf("Aliases: %s\n",*pp);
    for(pp=hostp->h_addr_list;*pp!=NULL;pp++)
    {
        addr.s_addr = (*(unsigned int *)*pp);
        printf("Address: %s\n",inet_ntoa(addr));
    }
    
    return 0;
}

通过gethostbyname和gethostbyaddr两个函数来实现。
阅读(1135) | 评论(2) | 转发(0) |
0

上一篇:网络地址转换

下一篇:多线程编程第一例

给主人留下些什么吧!~~