首页 > 代码库 > 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类静态方法