PackageCOM.SJF;Importjava.net.Inet4Address;Importjava.net.Inet6Address;Importjava.net.InetAddress;ImportJava.net.NetworkInterface;ImportJava.net.UnknownHostException;ImportJava.util.Enumeration;/** * * @author sjf0115 * * * * Public class inetaddressexample { Public Static void Main(string[] args) {//Get the network interfaces and associated for this host Try{NetworkInterface NetworkInterface =NULL;//Get information on each interface of the hostenumeration<networkinterface> interfacelist = Networkinterface.getnetworkinterfaces ();if(Interfacelist = =NULL) {System.out.println ("--no Interfaces found--"); }//if Else{ while(Interfacelist.hasmoreelements ()) {networkinterface = Interfacelist.nextelement ();//Interface nameSystem.out.println ("Interface->"+networkinterface.getname ());//Gets the address associated with the interface may contain IPV4 or IPV6 addresses depending on the host's configurationenumeration<inetaddress> inetaddresslist = networkinterface.getinetaddresses ();if(Inetaddresslist = =NULL) {System.out.println ("--no address for this networkinterface--"); }//if Else{InetAddress address =NULL; while(Inetaddresslist.hasmoreelements ()) {address = inetaddresslist.nextelement ();//detect each address to determine which IP address subclass belongs toSystem.out.print (AddressinstanceofInet4address?"(v4)": (AddressinstanceofInet6address?"(V6)":"(?)"));//Print IP addressSystem.out.println (":"+address.gethostaddress ()); }//while} }//while} }Catch(Exception e) {} String host ="Www.baidu.com";Try{//A name may be associated with multiple numeric addresses this method returns an instance of a set of all addresses associated with a given host nameinetaddress[] Addresseslist = inetaddress.getallbyname (host); for(InetAddress address:addresseslist) {System.out.println (Address.gethostname () +" "+address.gethostaddress ()); }//for //determines the IP address of a host, given the host ' s name.InetAddress Address2 = inetaddress.getbyname (host); System.out.println (Address2.gethostname () +" "+address2.gethostaddress ());//Returns the local host.InetAddress ADDRESS3 = Inetaddress.getlocalhost (); System.out.println (Address3.gethostname () +" "+address3.gethostaddress ()); }Catch(Unknownhostexception e) {E.printstacktrace (); } }}
[Java] Socket address inetaddress explanation