日期:2014-05-16 浏览次数:20966 次
/******************************************
根据ip得到主机名hostname
******************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.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("IP Address :%s\n",ipaddr);
return 0;
}