- 论坛徽章:
- 0
|
本帖最后由 markieff 于 2014-03-25 18:09 编辑
程序:用gethostbyaddr获取目的IP信息的程序。
程序在编译上没有问题,之前写过一个gethostbyname的程序,可以正常运行,但这个就不行。输入任何IP地址输出的都是:gethostbyaddr error for addr:加上IP,等于返回值是NULL,希望有大神帮忙看看为什么,困扰好久了,谢谢啦~
上程序:
#include <netdb.h>
#include <sys/socket.h>
#include <stdio.h>
int main(int argc, char **argv)
{
char *ptr, **pptr;
struct hostent *hptr;
char str[32];
char stadd[16];
ptr = argv[1];
if(!inet_aton(ptr,stadd)){
printf("inet_aton error\n");
return 1;
}
if((hptr = gethostbyaddr(stadd, sizeof(stadd), AF_INET)) == NULL);
{
printf("gethostbyaddr error for addr:%s\n", ptr);
herror("gethostbyaddr");
return 1;
}
printf("offical hostname:%s\n",hptr->h_name);
for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)
printf(" alias:%s\n",*pptr);
switch(hptr->h_addrtype)
{
case AF_INET:
case AF_INET6:
pptr=hptr->h_addr_list;
for(; *pptr!=NULL; pptr++)
printf(" address:%s\n",
inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
printf(" first address: %s\n",
inet_ntop(hptr->h_addrtype, hptr->h_addr, str, sizeof(str)));
break;
default:
printf("unknown address type\n");
break;
}
return 0;
} |
|