1,486
社区成员
发帖
与我相关
我的任务
分享
// 获取本机IP和名称
WORD wVersionRequested;
WSADATA wsaData;
struct addrinfo hostinfo, *hi;
int s;
wVersionRequested = MAKEWORD(2, 0);
memset(&hostinfo, 0, sizeof(struct addrinfo));
hostinfo.ai_family = AF_INET; // Allow IPv4
hostinfo.ai_socktype = SOCK_STREAM;
hostinfo.ai_flags = AI_ALL;
hostinfo.ai_protocol = IPPROTO_TCP;
hi = &hostinfo;
if (WSAStartup(wVersionRequested, &wsaData) == 0)
{
s = getaddrinfo(hostName, (PCSTR)m_port, &hostinfo, NULL);
if (s == 0)
{
hi = &hostinfo;
if (hi != NULL)
{
char str[INET_ADDRSTRLEN];
c_hostIP = inet_ntop(AF_INET, (struct in_addr*) hi->ai_addr, str, sizeof(str)); // 将IP以CString格式储存在封装变量中便于调用
}
}
freeaddrinfo(hi);
WSACleanup();
}