Dav*_*vre 2 java jvm network-programming network-interface
我正在使用 Windows 10,我想在我的计算机上插入一个设备(它创建了一个名为“Ethernet 12”和描述为“foobar”的新网络接口),并且能够使用 Java 设置该接口的 IP 地址。使用此链接:使用 JAVA 更改计算机 IP 地址,我可以配置接口的 IP 地址。我的问题是我无法在 Java 中检索设备创建的接口的名称。从我找到的文档中,我做了以下事情:
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while(networkInterfaces.hasMoreElements())
{
NetworkInterface networkInterface = networkInterfaces.nextElement();
if (networkInterface.isUp())
System.out.println("Display name: " + networkInterface.getDisplayName() + "\nName: " + networkInterface.getName() + "\nAddress: " + networkInterface.getInterfaceAddresses().get(0));
}
Run Code Online (Sandbox Code Playgroud)
并得到以下结果:
Display name: foobar
Name: eth15
Address 111.116.15.4
Run Code Online (Sandbox Code Playgroud)
我希望名称是“以太网 12”而不是“eth15”,这似乎没有任何意义。我注意到所有其他接口(wifi、以太网等)都一样。由于我需要我的接口(以太网 12)的真实名称来使用 netsh(而不是像“eth15”这样的随机地址)配置 IP 地址,我被卡住了......
是否有理由将“eth15”作为我的界面名称?
谢谢
是否有理由将“eth15”作为我的界面名称?
这需要一些挖掘。在 Java 世界中,NetworkInterfaces通过java.net.NetworkInterface::getNetworkInterfaces()哪些调用本机 java.net.NetworkInterface::getAll().
我们可以getAll()在此处找到OpenJDK 11 中的本机源代码,我们可以在其中找到以下注释:
/*
* Windows implementation of the java.net.NetworkInterface native methods.
* This module provides the implementations of getAll, getByName, getByIndex,
* and getByAddress.
*
* Interfaces and addresses are enumerated using the IP helper routines
* GetIfTable, GetIfAddrTable resp. These routines are available on Windows
* 98, NT SP+4, 2000, and XP. They are also available on Windows 95 if
* IE is upgraded to 5.x.
*
* Windows does not have any standard for device names so we are forced
* to use our own convention which is based on the normal Unix naming
* convention ("lo" for the loopback, eth0, eth1, .. for ethernet devices,
* tr0, tr1, .. for token ring, and so on). This convention gives us
* consistency across multiple Windows editions and also consistency with
* Solaris/Linux device names. Note that we always enumerate in index
* order and this ensures consistent device number across invocations.
*/
Run Code Online (Sandbox Code Playgroud)
所以曾几何时,当 Windows 95 还是一个东西时,有人决定不阅读实际的 Windows 界面名称,而是使用“我们自己的约定”。为什么?不知道。我本来希望得到一个单独的deviceName()和一个ourOwnConventionWhichHasNothingToDoWithTheActualNameDeviceName(),但不幸的是我没有被咨询。
底层 Windows API 调用GetIfTable确实以MIB_IFROW数组的形式返回每个接口的名称:
typedef struct _MIB_IFROW {
WCHAR wszName[MAX_INTERFACE_NAME_LEN];
IF_INDEX dwIndex;
IFTYPE dwType;
DWORD dwMtu;
DWORD dwSpeed;
DWORD dwPhysAddrLen;
UCHAR bPhysAddr[MAXLEN_PHYSADDR];
DWORD dwAdminStatus;
INTERNAL_IF_OPER_STATUS dwOperStatus;
DWORD dwLastChange;
DWORD dwInOctets;
DWORD dwInUcastPkts;
DWORD dwInNUcastPkts;
DWORD dwInDiscards;
DWORD dwInErrors;
DWORD dwInUnknownProtos;
DWORD dwOutOctets;
DWORD dwOutUcastPkts;
DWORD dwOutNUcastPkts;
DWORD dwOutDiscards;
DWORD dwOutErrors;
DWORD dwOutQLen;
DWORD dwDescrLen;
UCHAR bDescr[MAXLEN_IFDESCR];
} MIB_IFROW, *PMIB_IFROW;
Run Code Online (Sandbox Code Playgroud)
JVM 似乎读取了这个数组,然后生成一个“适当的” name,最后用生成的 name 覆盖 OS 提供的 name。
简而言之,它的实现看起来很多年都没有改变,如果不创建一个全新的 API,此时可能是不可能改变的。否则你可能会破坏人们的代码。
在我看来,有两种方法可以获取实际的接口名称:
GetIfTable自己进行本机调用。netsh并解析响应。这两种解决方案都有点难看,需要您确保在 Windows 上运行。
| 归档时间: |
|
| 查看次数: |
1661 次 |
| 最近记录: |