首页 > 代码库 > JAVA NetworkInterface类静态方法
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()); } }}
JAVA NetworkInterface类静态方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。