| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 263 人关注过本帖
标题:好难过啊。谁帮我分析下我写的代码。
只看楼主 加入收藏
wqte45
Rank: 1
等 级:禁止访问
帖 子:3
专家分:0
注 册:2007-4-25
收藏
 问题点数:0 回复次数:0 
好难过啊。谁帮我分析下我写的代码。
#include
#include
int main()
{
    char buff[255];
    WSAData wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData);
    hostent * hostaddr,*tmp;
     
    gethostname(buff, sizeof(buff));
    hostaddr=gethostbyname(buff);   
    printf("Host name is: %s\n", buff);
    printf("ip:%s\n",inet_ntoa(*(struct in_addr *)hostaddr->h_addr));
   
    while (1)
    {    tmp = gethostbyname(buff);
        if (inet_ntoa(*(struct in_addr *)hostaddr->h_addr) == inet_ntoa(*(struct in_addr *)tmp->h_addr))
            printf("ip 未改变:%s\n",inet_ntoa(*(struct in_addr *)tmp->h_addr));
        else
            printf("ip 改变为:%s\n",inet_ntoa(*(struct in_addr *)tmp->h_addr));
        Sleep(3000);
    }
        
    WSACleanup();
    return 0;
}


//本机ip:192.168.0.1
//运行本程序后 我把本机ip改为 192.168.0.2 后显示 "ip未改变:192.168.0.2"
  ip都改变了  if判断怎么还是等于啊
  
//有什么好的方法比较 两次gethostbyname(buff); 获得的地址差异。


我把 上面代码 修改为 printf("ip 未改变:%s %d %d\n",inet_ntoa(*(struct in_addr *)tmp->h_addr),hostaddr,hostaddr);
发现hostaddr和tmp 地址是一样的。 这是为什么呢 ??我不是从新运行了 tmp = gethostbyname(buff);了吗?

我又把上面的 代码替换了
if (strcmp(inet_ntoa(*(struct in_addr *)hostaddr->h_addr),inet_ntoa(*(struct in_addr *)tmp->h_addr))==0) 来做判断,
单卧运行后 更改ip后 为什么还是打印 "ip未改变:192.168.0.2"

准备提交了突然看到自己没分了。如果我有一千万分我就会给你们每个回复我的人一万分,我有吗?没有
所以了 抱歉 请大家帮帮我
搜索更多相关主题的帖子: include 
2012-03-30 18:35
快速回复:好难过啊。谁帮我分析下我写的代码。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019826 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved