2013-07-24 47 views
1

我有一個應用程序調用gethostbyname並在h_addr_list中顯示第一個IP地址。在安裝了VMware並且具有多個虛擬適配器的計算機上,我的應用程序始終會返回虛擬適配器之一的IP地址。我總是希望我的應用程序給我IP地址物理適配器。如何更改gethostbyname返回的IP地址列表順序?

因此,我想重新排序我的虛擬適配器,使物理適配器的IP地址應該在列表中第一位。我嘗試了所有的建議中 -

http://support.microsoft.com/kb/171320

http://support.microsoft.com/kb/894564/en-us#appliesto

他們沒有工作。我也在進行更改後重新啓動了我的系統。有沒有其他的方法來改變網絡適配器的順序?假設沒有,我怎樣才能確保通過我的代碼,我總是得到我的物理適配器的IP地址?

我的操作系統是Windows 8.這是我IPCONFIG/ALL輸出 -

Windows IP Configuration 

    Host Name . . . . . . . . . . . . : Win8x64D3b 
    Primary Dns Suffix . . . . . . . : 
    Node Type . . . . . . . . . . . . : Hybrid 
    IP Routing Enabled. . . . . . . . : No 
    WINS Proxy Enabled. . . . . . . . : No 
    DNS Suffix Search List. . . . . . : NESSINBLR.COM 

Ethernet adapter Ethernet: 

    Connection-specific DNS Suffix . : NESSINBLR.COM 
    Description . . . . . . . . . . . : Intel(R) 82566DM-2 Gigabit Network Connec 
tion 
    Physical Address. . . . . . . . . : 00-0F-FE-E7-00-A6 
    DHCP Enabled. . . . . . . . . . . : Yes 
    Autoconfiguration Enabled . . . . : Yes 
    Link-local IPv6 Address . . . . . : fe80::bc7c:6cf5:e8ed:c874%12(Preferred) 
    IPv4 Address. . . . . . . . . . . : 172.26.82.152(Preferred) 
    Subnet Mask . . . . . . . . . . . : 255.255.252.0 
    Lease Obtained. . . . . . . . . . : Wednesday, June 19, 2013 2:55:44 AM 
    Lease Expires . . . . . . . . . . : Friday, June 21, 2013 2:55:38 AM 
    Default Gateway . . . . . . . . . : 172.26.80.1 
    DHCP Server . . . . . . . . . . . : 172.26.116.52 
    DHCPv6 IAID . . . . . . . . . . . : 251662334 
    DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-18-3D-50-E5-00-0F-FE-E7-00-A6 

    DNS Servers . . . . . . . . . . . : 172.26.82.152 
    Primary WINS Server . . . . . . . : 172.26.82.152 
    NetBIOS over Tcpip. . . . . . . . : Enabled 

Ethernet adapter VMware Network Adapter VMnet1: 

    Connection-specific DNS Suffix . : 
    Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet 
1 
    Physical Address. . . . . . . . . : 00-50-56-C0-00-01 
    DHCP Enabled. . . . . . . . . . . : No 
    Autoconfiguration Enabled . . . . : Yes 
    Link-local IPv6 Address . . . . . : fe80::d59f:e785:bc67:74c4%17(Preferred) 
    IPv4 Address. . . . . . . . . . . : 192.168.3.1(Preferred) 
    Subnet Mask . . . . . . . . . . . : 255.255.255.0 
    Default Gateway . . . . . . . . . : 
    DHCPv6 IAID . . . . . . . . . . . : 251678806 
    DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-18-3D-50-E5-00-0F-FE-E7-00-A6 

    DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1 
             fec0:0:0:ffff::2%1 
             fec0:0:0:ffff::3%1 
    NetBIOS over Tcpip. . . . . . . . : Enabled 

Ethernet adapter VMware Network Adapter VMnet8: 

    Connection-specific DNS Suffix . : 
    Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet 
8 
    Physical Address. . . . . . . . . : 00-50-56-C0-00-08 
    DHCP Enabled. . . . . . . . . . . : No 
    Autoconfiguration Enabled . . . . : Yes 
    Link-local IPv6 Address . . . . . : fe80::3d00:a19f:32b5:5c52%18(Preferred) 
    IPv4 Address. . . . . . . . . . . : 192.168.5.1(Preferred) 
    Subnet Mask . . . . . . . . . . . : 255.255.255.0 
    Default Gateway . . . . . . . . . : 
    DHCPv6 IAID . . . . . . . . . . . : 268456022 
    DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-18-3D-50-E5-00-0F-FE-E7-00-A6 

    DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1 
             fec0:0:0:ffff::2%1 
             fec0:0:0:ffff::3%1 
    NetBIOS over Tcpip. . . . . . . . : Enabled 

Tunnel adapter isatap.{A80B07CA-01EF-411D-BB81-A4E2F06D588B}: 

    Media State . . . . . . . . . . . : Media disconnected 
    Connection-specific DNS Suffix . : 
    Description . . . . . . . . . . . : Microsoft ISATAP Adapter 
    Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0 
    DHCP Enabled. . . . . . . . . . . : No 
    Autoconfiguration Enabled . . . . : Yes 

Tunnel adapter isatap.NESSINBLR.COM: 

    Media State . . . . . . . . . . . : Media disconnected 
    Connection-specific DNS Suffix . : NESSINBLR.COM 
    Description . . . . . . . . . . . : Microsoft ISATAP Adapter #2 
    Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0 
    DHCP Enabled. . . . . . . . . . . : No 
    Autoconfiguration Enabled . . . . : Yes 

Tunnel adapter isatap.{8D6220B3-8F39-4514-8C4E-C79D8263C2B8}: 

    Media State . . . . . . . . . . . : Media disconnected 
    Connection-specific DNS Suffix . : 
    Description . . . . . . . . . . . : Microsoft ISATAP Adapter #3 
    Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0 
    DHCP Enabled. . . . . . . . . . . : No 
    Autoconfiguration Enabled . . . . : Yes 

我的應用程序總是返回192.168.5.1,而不是172.26.82.152

請幫助。

回答

1

我曾見過類似的東西。這並不完全回答你的問題,但這是我的。

remoteHost = gethostbyname(ac); 
int i = 0; 
if (remoteHost->h_addrtype == AF_INET) 
{ 
    //while (remoteHost->h_addr_list[i] != 0) 
    //{ 
    // addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++]; 
    //} 
    addr.s_addr = *(u_long *) remoteHost->h_addr_list[0]; 
} 

我已經評論了while循環。該部分只是在適配器列表上循環,最後停在最後一個(在我們的例子中是VMware適配器)。

使用h_addr_list [0]給出我的物理適配器。但是,我不確定訂單是如何確定的,或者我可以如何分辨哪些是物理的,哪些是虛擬的。

希望有所幫助。