familiar with InetAddress class: The InetAddress class is used primarily to get the specified network address,
InetAddress class does not have a direct explicit constructor。 To generate a InetAddress object, you must use a factory method that is available. Factory methods (Factory method) are only a static method in a class that returns a convention for an instance of that class. This is done in an overloaded constructor with a variety of argument lists, which makes the results clearer when you hold the unique method name.
InetAddress has three methods that can be used to create an instance of InetAddress
1.static inetaddress Getlocalhost () throws Unknownhostexception
2.static inetaddress getbyname (String hostName) throws Unknownhostexception
3.static inetaddress[] Getallbyname (String hostName) throws Unknownhostexception
non-static methods for the InetAddress class:
Boolean equals (Object other)
Returns True if the object has an Internet address that is the same as other.
Byte[] GetAddress ()
Returns the original IP address of this InetAddress object.
String gethostaddress ()
Returns a string of host addresses associated with the InetAddress object.
String GetHostName ()
Returns a string of host names associated with the InetAddress object.
int Hashcode ()
Returns the hash code of the calling object.
Boolean ismulticastaddress ()
Returns true if the Internet address is a multicast address, otherwise it returns false.
String toString ()
Returns the host name string and IP address.
Below we write a program to resolve the domain name:
package com.ip; import java.net.InetAddress; import java.net.UnknownHostException; import
Java.util.Scanner; public class IP {public static void main (String[]args) {try {System.out.print ("Enter the domain name to resolve:")
;
Scanner in=new Scanner (system.in);
String domainname=in.nextline ()//Enter the domain name to resolve System.out.println ("Use the InetAddress class method to get the IP address of the Web site" +domainname+ ... "); SYSTEM.OUT.PRINTLN ("Total IP number:" + inetaddress.getallbyname (domainname). length);//Receive
Number of IP inetaddress[] inetadd = inetaddress.getallbyname (domainname); Traverse all IP and output for (int i = 0; i < inetadd.length; i++) {System.out.println ("a" + (i)
+ 1) + "IP:" + inetadd[i]); The catch (Unknownhostexception e) {System.out.println ("Get the IP address of the Web site Www.csdn.net failed.") No corresponding IP.
"); }
}
}
Results: