Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
System.out.println(netInterface.getName());
Enumeration aa = netInterface.getInetAddresses();
while (aa.hasMoreElements()) {
ip = (InetAddress) aa.nextElement();
if (ip != null && ip instanceof Inet4Address) {
System.out.println("本机的IP = " + ip.getHostAddress());
}
}
}
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
System.out.println(netInterface.getName());
Enumeration aa = netInterface.getInetAddresses();
while (aa.hasMoreElements()) {
ip = (InetAddress) aa.nextElement();
if (ip != null && ip instanceof Inet4Address) {
System.out.println("本机的IP = " + ip.getHostAddress());
}
}
}
本文介绍如何使用Java代码遍历本地所有网络接口,并输出每个接口的名称及其对应的IPv4地址。
3957

被折叠的 条评论
为什么被折叠?



