Android (Java) Learning note 79:java in InetAddress class overview and use

Source: Internet
Author: User

To allow computers in your network to communicate with each other, you must specify an identification number for each computer, which specifies the computer to which you want to accept data and the computer that identifies the send.

In the TCP/IP protocol, This identification number is the IP address .

So, what if we get and manipulate IP addresses? For the convenience of our IP address acquisition and operation, Java provides a class inetaddress for us to use .

1  Packagecn.itcast_01;2 3 Importjava.net.InetAddress;4 Importjava.net.UnknownHostException;5 6 /*7 * If a class does not have a constructor method:8 * A: The members are all static (Math,arrays,collections)9 * B: Single case design mode (Runtime)Ten  * C: A static method in the class returns the object of the Class (inetaddress) * Class Demo {* * * Private demo () {}13 * * public static demo getXxx () {15 * Return new Demo (); *}17 *} -  *  + * See InetAddress's member Method: - * Public static inetaddress Getbyname (String host): Gets the IP Address object based on the hostname or the string representation of the IP address  +  */ A  Public classInetaddressdemo { at      Public Static voidMain (string[] args)throwsunknownhostexception { -         //Public static InetAddress Getbyname (String host) -         //inetaddress address = inetaddress.getbyname ("Liuyi"); -         //inetaddress address = inetaddress.getbyname ("192.168.12.92"); -InetAddress address = Inetaddress.getbyname ("192.168.12.63"); -  in        //Get two things: hostname, IP address of//public String gethostname () toString name =address.gethostname (); +         //Public String gethostaddress () -String IP =address.gethostaddress (); theSYSTEM.OUT.PRINTLN (name + "---" +IP); *     } $}

Android (Java) Learning note 79:java in InetAddress class overview and use

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.