Importjava.net.InetAddress;ImportJava.net.NetworkInterface;Importjava.net.SocketException;Importjava.net.UnknownHostException;Importjava.util.Enumeration; Public classNetworkinterfacestaticmethod { Public Static voidMain (string[] args) {Networkinterfacestaticmethod.interface_getbyindex (0); Networkinterfacestaticmethod.interface_getbyinetaddress ("10.11.3.160"); Networkinterfacestaticmethod.interface_getnetworkinterfaces (); } Public Static voidInterface_getbyindex (intindex) {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 voidinterface_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 voidInterface_getnetworkinterfaces () {enumeration<NetworkInterface> NIS =NULL; Try{NIS=networkinterface.getnetworkinterfaces (); } Catch(SocketException e) {e.printstacktrace (); } while(Nis.hasmoreelements ()) {System.out.println (nis.nextelement ()); } }}
JAVA NetworkInterface class static method