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* 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;兄弟们帮解决下呀,
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);
Unhandled exception in XXX.exe(WS2_32.DLL):0xC0000005:Access Violation为什么,第一次没问题,第2次就不行呢?
hostent *pHost = ::gethostbyname(hostname);
还是报错!~
........
sprintf(LocalIP, "%s&ComputerName:%s", LocalIP, name) ; 这样使用好像会出现 LocalIP 缓冲区溢出。
在我的XP下没问题,到了2000机器下问题就来了...
单步调试第3次循环到::gethostbyname(hostname);过不去,没想明白
但是,报错的地方并不是这里,而是gethostbyname(hostname);
出错的时候hostname里面的值是不是对的?