主机ip查找程序

来源:互联网 发布:战地2网络对战平台 编辑:程序博客网 时间:2024/05/01 19:27

《unix网络编程》看到第11章了,写了一个通过DNS从主机名查找主机IP的小程序,

测试环境:linux,

测试结果:

源码如下:

#include<stdio.h>#include <string.h>#include <netinet/in.h>#include <stdlib.h>#include <arpa/inet.h>#include <unp.h>#include <netdb.h>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){            //err_msg("gethostbyname error for host:%s %s",ptr,hstrerror(h_errno));            printf("FUCKING ERROR");            continue;        }        printf("正式主机名:%s\n",hptr->h_name);                for(pptr=hptr->h_aliases;*pptr!=NULL;pptr++){            printf("\talias=%s",*pptr);        }                switch(hptr->h_addrtype){            case AF_INET:                for(pptr=hptr->h_addr_list;*pptr!=NULL;pptr++){                    printf("\taddress:%s\n",inet_ntop(hptr->h_addrtype,*pptr,str,sizeof(str)));                }                break;            default:                printf("Fucking error");                break;        }            }    return 0;}

0 0
原创粉丝点击