要枚举所有可用的网络接口,您可以使用Java的NetworkInterface类。以下是一个示例代码,演示如何枚举所有可用的网络接口:
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class NetworkInterfaceExample {
public static void main(String[] args) {
try {
// 获取所有网络接口的枚举
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
// 遍历枚举并打印网络接口信息
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
System.out.println("Interface: " + networkInterface.getName());
System.out.println("Display Name: " + networkInterface.getDisplayName());
System.out.println("Hardware Address: " + networkInterface.getHardwareAddress());
System.out.println("MTU: " + networkInterface.getMTU());
System.out.println("Loopback: " + networkInterface.isLoopback());
System.out.println("Up: " + networkInterface.isUp());
System.out.println("Virtual: " + networkInterface.isVirtual());
System.out.println();
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}
在上面的示例中,我们使用NetworkInterface.getNetworkInterfaces()
方法获取所有网络接口的枚举。然后,我们遍历枚举并打印每个网络接口的相关信息,如名称、显示名称、硬件地址、MTU(最大传输单元)、是否为回环接口、是否启用等。
云+社区技术沙龙[第17期]
腾讯云“智能+互联网TechDay”华东专场
腾讯云“智能+互联网TechDay”华南专场
腾讯云“智能+互联网TechDay”西南专场
腾讯云数智驱动中小企业转型升级系列活动
云+社区沙龙online第5期[架构演进]
腾讯云“智能+互联网TechDay”华北专场
腾讯云Global Day LIVE
企业创新在线学堂
TVP技术夜未眠
领取专属 10元无门槛券
手把手带您无忧上云