import java.net.*; import java.util.Enumeration; import java.util.List; public class test_03 { public static void main(String[] args) throws Exception { //InterfaceAddress类的使用: //对应的是网络接 //1:getAddress()得到对应的InetAddress对象,getBroadcast(),ipv4得到广播地址的InetAddress对象 //method_01(); //点对点设备 //method_02(); //是否支持多播 //method_03(); //NetworkInterface除getNerworkInterfaces()方法外的三个静态方法 //根据索引获取NetworkInterface对象:getByIndex() method_04(); } private static void method_01() throws SocketException { Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces(); while (networkInterfaceEnumeration.hasMoreElements()){ NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement(); System.out.println("网络设备名称:"+networkInterface.getName()); System.out.println("网络设备显示名称:"+networkInterface.getDisplayName()); List<InterfaceAddress> interfaceAddresses = networkInterface.getInterfaceAddresses(); for (int i = 0; i < interfaceAddresses.size(); i++) { InterfaceAddress interfaceAddress = interfaceAddresses.get(i); InetAddress inetAddress = interfaceAddress.getAddress(); if(inetAddress!=null) System.out.println("\tinterfaceAddress.getAddress():"+inetAddress.getHostAddress()); inetAddress = interfaceAddress.getBroadcast(); if(inetAddress!=null) System.out.println("\tinterfaceAddress.getBroadcast():"+inetAddress.getHostAddress()); System.out.println("\tinterfaceAddress.getNetworkPrefixLength():"+interfaceAddress.getNetworkPrefixLength()); } } } private static void method_02() throws SocketException { Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces(); while (networkInterfaceEnumeration.hasMoreElements()){ NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement(); if (networkInterface.isPointToPoint()){ System.out.println("网络设备名称:"+networkInterface.getName()); System.out.println("网络设备显示名称:"+networkInterface.getDisplayName()); System.out.println("是否为点对点设备:"+networkInterface.isPointToPoint()); } } } private static void method_03() throws SocketException{ Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces(); while (networkInterfaceEnumeration.hasMoreElements()){ NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement(); System.out.println("网络设备名称:"+networkInterface.getName()); System.out.println("网络设备显示名称:"+networkInterface.getDisplayName()); System.out.println("网络设备是否支持多播:"+networkInterface.supportsMulticast()); System.out.println("\n"); } } private static void method_04() throws SocketException, UnknownHostException { Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces(); while (networkInterfaceEnumeration.hasMoreElements()){ NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement(); System.out.println(networkInterface.getName()); System.out.println(networkInterface.getDisplayName()); System.out.println(networkInterface.getIndex()); System.out.println("\n"); } //上面代码可知1为lo System.out.println("----->"+NetworkInterface.getByIndex(1)); System.out.println("----->"+NetworkInterface.getByName("lo")); System.out.println("----->"+NetworkInterface.getByInetAddress(InetAddress.getByName("localhost"))); /*最后的输出: ----->name:lo (Software Loopback Interface 1) ----->name:lo (Software Loopback Interface 1) ----->name:lo (Software Loopback Interface 1) * */ } }
7天掌握NIO和SOCKET,第五天,InterfaceAddress类的使用:
最新推荐文章于 2022-12-12 21:39:02 发布