JAVA NetworkInterface类静态方法
import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Enumeration; public class networkInterfaceStaticMethod { public static void main(String[] args) { networkInterfaceStaticMethod.interface_getByIndex(0); networkInterfaceStaticMethod.interface_getByInetAddress("10.11.3.160"); networkInterfaceStaticMethod.interface_getNetworkInterfaces(); } public static void interface_getByIndex(int index) { NetworkInterface ni = null; try { ni = NetworkInterface.getByIndex(index); } catch (SocketException e) { e.printStackTrace(); } System.out.println((ni == null) ? "interface not exist!" : ni.getName() + " : " + ni.getDisplayName() + " : " + ni.getIndex()); } public static void interface_getByInetAddress(String host) { InetAddress local; NetworkInterface ni = null; try { local = InetAddress.getByName(host); ni = NetworkInterface.getByInetAddress(local); } catch (UnknownHostException e) { e.printStackTrace(); } catch (SocketException e) { e.printStackTrace(); } Enumeration<InetAddress> addresses = ni.getInetAddresses(); while(addresses.hasMoreElements()) { System.out.println(addresses.nextElement().getHostAddress()); } } public static void interface_getNetworkInterfaces() { Enumeration<NetworkInterface> nis = null; try { nis = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { e.printStackTrace(); } while(nis.hasMoreElements()) { System.out.println(nis.nextElement()); } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。