c++すべてのローカル接続IPを取得
4705 ワード
- std::string VengFuncConfig::GetLocalHostIp(void)
- {
- vector vLocalIpList;
- string sLocalIP;
- sLocalIP.clear();
- WSADATA wsaData;
- char name[155]={0};
- if ( WSAStartup( MAKEWORD(2,0), &wsaData ) == 0 )
- {
- if(gethostname(name,sizeof(name)) == 0)
- {
- struct hostent* pHost = gethostbyname(name);
- if (0 == pHost )
- {
- return sLocalIP;
- }
- for (int i = 0; pHost->h_addr_list[i] != 0; ++i)
- {
- struct in_addr addr;
- memcpy(&addr, pHost->h_addr_list[i], sizeof(struct in_addr));
- vLocalIpList.push_back(inet_ntoa(addr));
- }
- }
- WSACleanup( );
- }
- // IP vLocalIpList
- int nLength = vLocalIpList.size();
- // 10.10. IP
- for(int i=0;i < nLength;i++)
- {
- if (vLocalIpList[i].find("10.10.") != -1)
- {
- sLocalIP = vLocalIpList[i];
- }
- }
- // IP, IP
- if (sLocalIP.empty())
- {
- for(int i=0;i < nLength;i++)
- {
- if (vLocalIpList[i].find("192.168.") == -1)
- {
- sLocalIP = vLocalIpList[i];
- }
- }
- }
-
- return sLocalIP;
- }