gethostbyname获取64位系统ip报错
先说环境:
操作系统:windows server 2008 enterprise x64
编译环境:VS2010
项目属性:活动解决方案平台 x64; 目标计算机:MachineX86 (/MACHINE:X86); 结构成员对齐:1 字节 (/Zp1)
调试时出现的错误:Broadcast.exe 中的 0x000000013f95344c 处有未经处理的异常: 0xC0000005: 读取位置 0xffffffffffffffff 时发生访问冲突
问题:
1:VS2010 默认结构体多少字节对齐?对齐方式设置后系统的结构体,如:struct hostent 怎么对齐?监视窗口显示的地址怎么解释?
2:gethostbyname函数与操作系统,以及编译器(vs2010)的配置环境 x64 有没有关系?
窗口 “监视1” 的截图:
源代码如下:
BOOL CBroadcastService::OnInit()
{
WSADATA wsd;
if( WSAStartup( MAKEWORD(2,2),&wsd) != 0 )
{
printf("WSAStartup() failed\n");
return -1;
}
char m_szHostName[255] = {};
PHOSTENT pstHostInfo;
struct in_addr addr[8];
int nGetIPOK = 0;
charszDBServer[18];
char*p = NULL;
intn = 0;
charszLog[1024];
SYSTEMTIME CurTime;
char szIP[16];
memset(szIP,0,sizeof(szIP));
char *pszTemp = NULL;
intnIsInOneNet = 1;
struct hostent *remoteHost;
char host_name[255] = {};
struct in_addr aaddr;
memset(&aaddr,0,sizeof(in_addr));
gethostname(host_name, 255);
printf("Calling gethostbyname with %s\n", host_name);
remoteHost = gethostbyname(host_name);
if (remoteHost)
{
int i = 0;
if (remoteHost->h_addrtype == AF_INET)
{
while (remoteHost->h_addr_list[i]) //出错句,指针显示不为空,为什么不能继续运行而是报错?
{
i = 0;
aaddr.s_addr = *(u_long *) remoteHost->h_addr_list[i++];
printf("\tIP Address #%d: %s\n", i, inet_ntoa(aaddr));
}
}
}
//省略...
}