【发布时间】:2020-01-29 04:38:37
【问题描述】:
我有一个从网络接口提取信息的 java 应用程序,我正在使用 NetworkInterface 类来遍历我计算机中的大量接口。 “我已经安装了 virtualbox”,这会创建许多虚拟界面。但我需要从我的无线或以太网接口获取信息,因为它们是连接到网络的唯一选择。问题是我希望能够对所有这些接口进行排序并只获得相关的。你们建议什么方法?通过 NetworkInterface 集合搜索 eth0 或 wlan1。任何想法表示赞赏。这是我的应用程序中处理网络接口的代码。
//displaying all network interface
Enumeration<NetworkInterface> nets = null;
try {
nets = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e3) {
e3.printStackTrace();
}
for (NetworkInterface netint : Collections.list(nets)) {
displayInterfaceInformation(netint);
}
static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
System.out.printf(count + " Display name: %s\n", netint.getDisplayName());
System.out.printf("Name: %s\n", netint.getName());
for (InetAddress inetAddress : Collections.list(inetAddresses)) {
System.out.printf("InetAddress: %s\n", inetAddress);
}
System.out.println("\n");
}
【问题讨论】:
-
您是专门寻找 eth0 或 wlan1 还是只是连接到互联网的接口?
-
无论连接是使用 eth0 还是 wlan1 完成,代码都应该工作。但是,是的,它仅显示已连接接口的信息。即具有有效的 IP 地址
标签: java networking network-programming network-interface