import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;
public class ListNets
{
public static void main(String args[]) throws SocketException {
Enumeration nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets))
displayInterfaceInformation(netint);
}
static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
out.printf("Display name: %s/n", netint.getDisplayName());
out.printf("Name: %s/n", netint.getName());
Enumeration inetAddresses = netint.getInetAddresses();
for (InetAddress inetAddress : Collections.list(inetAddresses)) {
out.printf("InetAddress: %s/n", inetAddress);
}
out.printf("/n");
}
}
执行结果:
Display name: bge0
Name: bge0
InetAddress: /fe80:0:0:0:203:baff:fef2:e99d%2
InetAddress: /121.153.225.59
Display name: lo0
Name: lo0
InetAddress: /0:0:0:0:0:0:0:1%1
InetAddress: /127.0.0.1
监听网络地址
最新推荐文章于 2024-05-12 14:48:00 发布
本文介绍了一段Java代码,该代码用于枚举并显示本地计算机上的所有网络接口及其相关信息,如展示名称、接口名称和IP地址等。
1829

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



