首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

gethostbyname报错!有点莫名其妙解决方案

2012-01-31 
gethostbyname报错!有点莫名其妙charname[20]char*LocalIPIPERROR&NameERROR hostent*hostinfoNULL

gethostbyname报错!有点莫名其妙
char     name[20];
    char*   LocalIP   =   "IPERROR&NameERROR ";      
    hostent   *   hostinfo=NULL;      
    if   (   gethostname(name,   sizeof(name))==0   )      
    {      
          hostinfo   =   gethostbyname(name);     //这行报错!异常  
      if(hostinfo   !=   NULL)      
      {      
          LocalIP   =   inet_ntoa   (*(struct   in_addr   *)*hostinfo-> h_addr_list);      
      }  
          sprintf(LocalIP,   "%s&ComputerName:%s ",   LocalIP,   name)   ;  
    }
    return   LocalIP;

兄弟们帮解决下呀,

[解决办法]
char hostname[20];
gethostname(hostname,20);

//get ipv4 address.
hostent *pHost = ::gethostbyname(hostname);
in_addr addr;
char *p = pHost-> h_addr_list[0];
memcpy(&addr.S_un.S_addr,p,pHost-> h_length);
CString v4IP = ::inet_ntoa(addr);
[解决办法]
char* LocalIP = "IPERROR&NameERROR ";
........
sprintf(LocalIP, "%s&ComputerName:%s ", LocalIP, name) ;

这样使用好像会出现 LocalIP 缓冲区溢出。

[解决办法]
缓冲区溢出的话就可能引起连锁反应了,这个地方你改过来还是报错吗?
出错的时候hostname里面的值是不是对的?

热点排行