没有装虚拟机,获取IP地址很正常,但是装上虚拟机之后后,或者有多张网卡的情况,如何来正确IP地址(能够连接公网的那个IP)??高手指点!

解决方案 »

  1.   


    BOOL CIocpServerSocket::GetLocalIps(CString &RetIps)
    {

    vector<char *> vtIps;

    // ASSERT you have called WSAStartup to enable the following functions.
    WORD wVersionRequested; 
    WSADATA wsaData; 
    int err; 
    wVersionRequested = MAKEWORD( 2, 2 ); 
    err = WSAStartup( wVersionRequested, &wsaData ); 
    if ( err != 0 ) 

    cout << "WSAStartup failed !" << endl; 
    return false; 
    }

    char szhn[256]; 
    int nStatus = gethostname(szhn, sizeof(szhn)); 
    if (nStatus == SOCKET_ERROR ) 

    cout << "gethostname failed, Error code: " << WSAGetLastError() << endl; 

    WSACleanup(); 
    return FALSE;

    HOSTENT *host = gethostbyname(szhn); 
    if (host != NULL) 

    for ( int i=0; ; i++ ) 

    vtIps.push_back( inet_ntoa( *(IN_ADDR*)host-> h_addr_list[i] ) ) ; 
    if ( host-> h_addr_list[i] + host-> h_length >= host-> h_name ) 
    break; 



    for(vector<char *>::iterator it = vtIps.begin(); it != vtIps.end(); it++)
    {
    RetIps = RetIps + CString(*it) + " ";
    }

    WSACleanup(); 
    return TRUE;
    }
      

  2.   

    GetAdaptersInfo()获取所有的ip,自己再找