免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4049 | 回复: 6
打印 上一主题 下一主题

[C] 关于gethostbyaddr的返回值永远是NULL [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-03-25 18:09 |只看该作者 |倒序浏览
本帖最后由 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;
}

论坛徽章:
0
2 [报告]
发表于 2014-03-26 12:17 |只看该作者
我已经没有vc开发环境了,建议找个人家写好的例子,或者msdn的例子,移植过来试试。

论坛徽章:
0
3 [报告]
发表于 2014-03-26 15:29 |只看该作者
回复 2# Hugo801122


    我试过其他人写的测试程序,也是不行..

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
4 [报告]
发表于 2014-03-26 15:59 |只看该作者
windows要先初始化网络环境
WSAStartup

论坛徽章:
0
5 [报告]
发表于 2014-03-26 18:42 |只看该作者
回复 4# folklore
呃,我是在linux的虚拟机下运行的

   

论坛徽章:
0
6 [报告]
发表于 2014-03-29 11:20 |只看该作者
if((hptr = gethostbyaddr(stadd, sizeof(stadd), AF_INET)) == NULL);

多了一个; 号

论坛徽章:
11
巨蟹座
日期:2013-12-23 11:12:14双子座
日期:2014-08-28 09:14:55子鼠
日期:2014-07-25 16:21:22摩羯座
日期:2014-07-23 15:17:47摩羯座
日期:2014-05-30 13:09:05午马
日期:2014-04-30 18:10:00天秤座
日期:2014-04-25 12:12:00申猴
日期:2014-04-22 11:30:15午马
日期:2014-03-07 16:06:40辰龙
日期:2013-12-25 18:36:00摩羯座
日期:2014-09-02 17:00:55
7 [报告]
发表于 2014-03-29 18:48 |只看该作者
...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP