监听网络地址
来源:互联网 发布:穿越火线网络异常23-2 编辑:程序博客网 时间:2024/05/01 13:22
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: bge0Name: bge0InetAddress: /fe80:0:0:0:203:baff:fef2:e99d%2InetAddress: /121.153.225.59Display name: lo0Name: lo0InetAddress: /0:0:0:0:0:0:0:1%1InetAddress: /127.0.0.1