用的是gethostbyname方法,获取到的IP有好几个,有我当前本地连接的IP地址,还有虚拟机的两个IP地址,甚至还有我以前删除过的本地连接的IP地址...
请问我如何能区分呢??
我只想要我当前使用的IP地址,谢谢帮忙!!!

解决方案 »

  1.   

    GetAdaptersInfo// It is possible for an adapter to have multiple
    // IPv4 addresses, gateways, and secondary WINS servers
    // assigned to the adapter. 
    // Note that this sample code only prints out the 
    // first entry for the IP address/mask, gateway,
    // and secondary WINS server for each adapter. PIP_ADAPTER_INFO pAdapterInfo;
    PIP_ADAPTER_INFO pAdapter = NULL;
    DWORD dwRetVal = 0;/* variables used to print DHCP time info */
    struct tm newtime;
    char buffer[32];    
    errno_t error;pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) );
    ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);// Make an initial call to GetAdaptersInfo to get
    // the necessary size into the ulOutBufLen variable
    if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
      free(pAdapterInfo);
      pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen); 
      if (pAdapterInfo == NULL) {
        printf("Error allocating memory needed to call GetAdaptersinfo\n");
        return 1;
    }if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
      pAdapter = pAdapterInfo;
      while (pAdapter) {
        printf("\tAdapter Name: \t%s\n", pAdapter->AdapterName);
        printf("\tAdapter Desc: \t%s\n", pAdapter->Description);
        printf("\tAdapter Addr: \t");
        for (UINT i = 0; i < pAdapter->AddressLength; i++) {
          if (i == (pAdapter->AddressLength - 1))
             printf("%.2X\n",(int)pAdapter->Address[i]);
          else
            printf("%.2X-",(int)pAdapter->Address[i]);
        }
        printf("\tIP Address: \t%s\n", pAdapter->IpAddressList.IpAddress.String);
        printf("\tIP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String);    printf("\tGateway: \t%s\n", pAdapter->GatewayList.IpAddress.String);
        printf("\t***\n");    if (pAdapter->DhcpEnabled) {
          printf("\tDHCP Enabled: Yes\n");
          printf("\t\tDHCP Server: \t%s\n", pAdapter->DhcpServer.IpAddress.String);      printf("\tLease Obtained: "); 
          /* Display local time */
          error = _localtime32_s(&newtime, &pAdapter->LeaseObtained); 
          if (error)
            printf("\tInvalid Argument to _localtime32_s\n");
          else {
            // Convert to an ASCII representation 
            error = asctime_s(buffer, 32, &newtime);
            if (error)
              printf("Invalid Argument to asctime_s\n");
            else 
              /* asctime_s returns the string terminated by \n\0 */
              printf("%s", buffer);
          }      printf("\tLease Expires:  ");
          error = _localtime32_s(&newtime, &pAdapter->LeaseExpires); 
          if (error)
            printf("Invalid Argument to _localtime32_s\n");
          else {
            // Convert to an ASCII representation 
            error = asctime_s(buffer, 32, &newtime);
            if (error)
              printf("Invalid Argument to asctime_s\n");
            else  
              /* asctime_s returns the string terminated by \n\0 */
              printf("%s", buffer);
          }
        }
        else
          printf("\tDHCP Enabled: No\n");
        
        if (pAdapter->HaveWins) {
          printf("\tHave Wins: Yes\n");
          printf("\t\tPrimary Wins Server: \t%s\n", pAdapter->PrimaryWinsServer.IpAddress.String);
          printf("\t\tSecondary Wins Server: \t%s\n", pAdapter->SecondaryWinsServer.IpAddress.String);
        }
        else
          printf("\tHave Wins: No\n");
        pAdapter = pAdapter->Next;
      }
    }
    else {
      printf("GetAdaptersInfo failed with error: %d\n", dwRetVal);if (pAdapterInfo)
      free(pAdapterInfo);
    }
    如果你机器接了多块网卡,还都能连上网,没啥好办法区分一下,就都找出来,一个一个对应一下吧,要用哪个,选择一下!
      

  2.   

    感谢#1楼兄弟的回答我测试了下,发现能取出所有的IP,这个功能我已经实现了的
    我需要的就是用代码挑出我要的IP地址,不可能让人去自己选择要哪个的。