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