Chinaunix首页 | 论坛 | 博客
  • 博客访问: 68647
  • 博文数量: 32
  • 博客积分: 2024
  • 博客等级: 大尉
  • 技术积分: 305
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-16 15:05
文章分类

全部博文(32)

文章存档

2009年(32)

我的朋友
最近访客

分类: 系统运维

2009-03-24 09:26:28

调试环境:Fedora 8

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

/*gethostbyname(),structhostent,hstrerror(),h_errno*/
#include <stdlib.h>                    /*exit()*/
#include <arpa/inet.h>                /*inet_ntop*/

int
main(int argc, char **argv)
{
    char *ptr, **pptr;
    char str[INET_ADDRSTRLEN];
    struct hostent *hptr;

    while(--argc > 0){
        ptr = *++argv;
        if((hptr = gethostbyname(ptr)) == NULL){
         printf("gethostbyname error for host %s: %s\n",
             ptr, hstrerror(h_errno));
         continue;
        }
        printf("official hostname %s\n", hptr->h_name);
    
        for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)
         printf("\talias: %s\n", *pptr);

        printf("The length of address is: %d of %s\n", hptr->h_length, hptr->h_name);

        switch(hptr->h_addrtype){
         case AF_INET:
                pptr = hptr->h_addr_list;
                for(; *pptr != NULL; pptr++)
                    printf("\taddress: %s\n",
                        inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
                break;
            default:
                printf("unknown address type\n");
                break;
        }
    }
    exit(0);
}

阅读(612) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~