获得本机IP,并且将ip放在CIpAdress里

时间:2022-01-17 14:25:07

 

 

char szHostName[MAX_PATH + 1];
gethostname(szHostName, MAX_PATH); //得到计算机名
hostent *p = gethostbyname(szHostName); //从计算机名得到主机信息
char *pIP1 = inet_ntoa(*(in_addr *)p->h_addr_list[0]);//将32位IP转化为字符串IP
DWORD dwip = inet_addr(pIP1);
unsigned char *pIP = (unsigned char*)&dwip;
m_ipAddr.SetAddress(*pIP, *(pIP + 1), *(pIP + 2), *(pIP + 3));//控件变量设置IP地址

 

 

ps:  如果gethostname返回10093,说明没有初始化winsock

原文:Either the application has not called WSAStartup, or WSAStartup failed.

解决:

WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);