string GetLocalIpAddress()
{
WORD wVersionRequested =
MAKEWORD(2, 2);
WSADATA wsaData;
if
(WSAStartup(wVersionRequested, &wsaData) !=
0)
return
"";
char local[255] = {0};
gethostname(local,
sizeof(local));
hostent* ph =
gethostbyname(local);
if (ph == NULL)
return
"";
in_addr addr;
memcpy(&addr,
ph->h_addr_list[0], sizeof(in_addr)); //
这里仅获取第一个ip
string localIP;
localIP.assign(inet_ntoa(addr));
WSACleanup();
return localIP;
}
|