Linux下c实现 IP转为域名

来源:互联网 发布:大型网络监控怎么安装 编辑:程序博客网 时间:2024/05/17 23:13
都是入门级的例子
练手而已

/***************************************************************************
 *   Copyright (C) 2008 by root   *
Linux下c实现   IP转为域名
 ***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

int main(int argc, char *argv[])
{
    struct sockaddr_in addr;
    struct hostent *host;
    char ipaddr[40];
    printf("请输入IP/n");
    scanf("%s",ipaddr);
    getchar();
    if(inet_aton(ipaddr,&addr.sin_addr)!=0)
    {
        host=gethostbyaddr((char *)&addr.sin_addr,4,AF_INET);
       
    }
    if(host==NULL)
    {
        fprintf(stderr,"NO address information of host %s/n",ipaddr);
        exit(1);
    }
   
    printf("HostName :%s/n",host->h_name);
    printf("主机别名: %s/n",host->h_aliases);
    printf("IP Address :%s/n",ipaddr);
    return 0;
   
}
原创粉丝点击