Instance methods for InetAddress objects

Source: Internet
Author: User

Importjava.net.Inet4Address;Importjava.net.Inet6Address;Importjava.net.InetAddress;Importjava.net.UnknownHostException; Public classInetaddressmethod { Public Static voidMain (string[] args) {Inetaddressmethod inetaddr=NewInetaddressmethod ();        Inetaddr._gethostaddress (); Inetaddr._getaddress ("Www.csdn.net"); Inetaddressmethod._getiptype ("00::01"); }         Public void_gethostaddress () {inetaddress Ipv4address1=NULL; Try{Ipv4address1= Inetaddress.getbyname ("1.2.3.4"); } Catch(unknownhostexception e) {e.printstacktrace ();        } System.out.println (IPV4ADDRESS1);    System.out.println (Ipv4address1.gethostaddress ()); }             Public void_getaddress (String hostname) {inetaddress address=NULL; Try{Address=Inetaddress.getbyname (hostname); } Catch(unknownhostexception e) {e.printstacktrace (); }                byte[] IP =address.getaddress ();  for(byteIpsegment:ip) {System.out.print (ipsegment+ "."); } System.out.println ("");  for(byteIpsegment:ip) {            intNewipsegment = (Ipsegment < 0)? (256 +ipsegment): ipsegment; System.out.print (Newipsegment+ "."); }    }             Public Static void_getiptype (String hostname) {inetaddress address=NULL; Try{Address=Inetaddress.getbyname (hostname); } Catch(unknownhostexception e) {e.printstacktrace ();                } System.out.println (Address.gethostaddress ()); Switch(Address.getaddress (). length) { Case4: System.out.println ("IPv4 address!");  Break;  Case16: System.out.println ("IPv6 address!");  Break; }                if(Addressinstanceofinet4address) {System.out.println ("IPv4 address!"); }        if(Addressinstanceofinet6address) {System.out.println ("IPv6 address!"); }    }}

Instance methods for InetAddress objects

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.