[Java] Socket address inetaddress explanation

Source: Internet
Author: User

 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.