An IP address is an address having information about how to reach a specific host which is a 32-bit unique address number having an address space of 2^32. InetAddress class is a representation of an IP address. It represents both the 32-bit IPv4 address and the 128-bit IPv6 address. It is the superclass of Inet6Address and Inet4Address classes. An instance of this class consists of an IP address and usually a hostname depending on whether hostname resolution was performed during the creation.
Note: There are no constructors for this class but static methods which return instances of InetAddress class for general use
Methods of InetAddress Class
Method | Action Performed |
---|---|
equals() | Returns true if this IP address is the same as that of the object specified. Equals() method don’t consider hostnames while comparing and only consider IP address associated. |
getAddress() | Returns the raw IP address of this InetAddress object as an array. The order in which bytes appear in an array is the same as in IP address i.e. getAddress[0] will contain the highest order byte. |
getByAddress() | Create an InetAddress object. It takes the hostname and IP address as its parameter. The hostname can be the machine name as in “www.geeksforgeeks.org” or its textual IP address. |
getByName() | Returns the IP Address of the host specified. If the host is a literal IP address, then only its validity is checked. |
getAllByName() | Returns an array of IP addresses for the given host |
getLoopbackAddress() | Returns the loopback address |
getHostAddress() | Returns IP address in textual form. |
getHostName() | Returns the hostname for this IP Address. If this object was created with a hostname then it is returned, otherwise, a reverse lookup is performed to return the system configured hostname. |
getLocalHost() | Returns the IP address of the local host. |
getCanonicalHostName() | Returns the fully qualified domain name for this object. If this object was created with a hostname then it is returned, otherwise, a reverse lookup is performed to return the system configured hostname. |
hashCode() | Returns the hashcode associated with this address object. |
isAnyLocalAddress() | Returns true if this address represents a local address. |
isLinkLocalAddress() | Returns true if this address is a link-local address. |
isLoopbackAddress() | Returns true if this address is a loopback address. |
isMCGlobal() | Returns true if this multicast address has global scope. |
isMCLinkLocal() | Returns true if this multicast address has link scope. |
isMCNodeLocal() | Returns true if this multicast address has node scope. |
isMCOrgLocal() | Returns true if this multicast address has organization scope. |
isMCSiteLocal() | Returns true if this multicast address has site scope. |
isMulticastAddress() | Returns true if this address is an IP multicast address. Multicast addresses have 1110 as their first 4 bits. |
isReachable() | Returns true if this address is reachable. ICMP echo requests are used if permission can be granted otherwise the host tries to make a TCP connection at port 7 of the destination. This method is used generally as a pre-condition in various programs, to avoid Host Unreachable exceptions in the future |
isReachable() | Specify the network interface to be used while checking for reachability and the ttl parameter specifies the number of hops the echo packet makes before exiting the network. |
isSiteLocalAddress() | Returns true if this address is a site-local address. |
toString() | Converts the IP address to the string. It returns the result as hostname / IP address. |
Example:
Java
// Java Program to Illustrate Methods of Inetaddress Class // Importing required classes import java.net.Inet4Address; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; // Main class public class GFG { // Main driver method public static void main(String[] args) throws UnknownHostException { // Input URL String url = "www.geeksforgeeks.org" ; byte addr[] = { 127 , 0 , 0 , 1 }; InetAddress ip1 = Inet4Address.getByName(url); InetAddress ip2 = InetAddress.getByAddress(addr); // Following methods checks the property // of the thus created object // getAddress() method System.out.println( "Address : " + Arrays.toString(ip1.getAddress())); // getHostAddress() method System.out.println( "Host Address : " + ip1.getHostAddress()); // isAnyLocalAddress() method System.out.println( "isAnyLocalAddress : " + ip1.isAnyLocalAddress()); // isLinkLocalAddress() method System.out.println( "isLinkLocalAddress : " + ip1.isLinkLocalAddress()); // isLoopbackAddress() method System.out.println( "isLoopbackAddress : " + ip1.isLoopbackAddress()); // isMCGlobal() method System.out.println( "isMCGlobal : " + ip1.isMCGlobal()); // isMCLinkLocal() method System.out.println( "isMCLinkLocal : " + ip1.isMCLinkLocal()); // isMCNodeLocal() method System.out.println( "isMCNodeLocal : " + ip1.isMCNodeLocal()); // isMCOrgLocal() method System.out.println( "isMCOrgLocal : " + ip1.isMCOrgLocal()); // isMCSiteLocal() method System.out.println( "isMCSiteLocal : " + ip1.isMCSiteLocal()); // isMulticastAddress() method System.out.println( "isMulticastAddress : " + ip1.isMulticastAddress()); // isSiteLocalAddress() method System.out.println( "isSiteLocalAddress : " + ip1.isSiteLocalAddress()); // hashCode() method System.out.println( "hashCode : " + ip1.hashCode()); // equals() method System.out.println( "ip1==ip2 : " + ip1.equals(ip2)); } } |
Output:
Example 2:
Java
// Java Program to Illustrate Methods of Inetaddress Class // Importing required classes import java.io.IOException; import java.net.InetAddress; import java.util.Arrays; // Main class public class GFG { // Main driver method public static void main(String[] args) throws IOException { // Input sample URL String url = "www.geeksforgeeks.org" ; byte addr[] = { 127 , 0 , 0 , 1 }; // getByName() method InetAddress ip1 = InetAddress.getByName(url); System.out.println( "getByName() : " + ip1); // getByAddress() method InetAddress ip2 = InetAddress.getByAddress(addr); System.out.println( "getByAddress() : " + ip2); // getLocalHost() method InetAddress ip3 = InetAddress.getLocalHost(); System.out.println( "getLocalHost() : " + ip3); // getLoopbackAddress() method InetAddress ip4 = InetAddress.getLoopbackAddress(); System.out.println( "getLoopbackAddress() : " + ip4); // getAllByName() method // Returns all ip addresses associated with // 'google.com' InetAddress addrs[] = InetAddress.getAllByName( "www.google.com" ); System.out.println( "Google ip addresses : " + Arrays.toString(addrs)); // isReachable() method boolean isreach = ip1.isReachable( 50 ); System.out.println( "ip1 isReachable() : " + isreach); // getHostname() method String hostname = ip1.getHostName(); System.out.println( "ip1 hostname :" + hostname); // getCanonicalHostname() method System.out.println( "ip1 CanonicalHostname : " + ip1.getCanonicalHostName()); // toString() method System.out.println( "ip1 toString() : " + ip1.toString()); } } |
Output:
Implementation: The following program uses InetAddress class to get the IP address of the given domain name. When the program is run on a system connected to the Internet, it gives the IP address(es) of the domain given.
Example:
Java
// Java program to Demonstrate Working of InetAddress Class // by Finding IP address for a Domain Name // Importing required classes import java.net.*; // Main class // GetIPAddress public class GFG { // Main driver method public static void main(String args[]) throws Exception { // Input sample URL String url = "www.google.com" ; // Try block to check for exceptions try { // Getting IP addresses related to the domain InetAddress ips[] = InetAddress.getAllByName(url); // Displaying IP addresses System.out.println( "IP Address(es)" ); for (InetAddress addr : ips) System.out.println(addr.getHostAddress()); } // Catch block to handle exceptions catch (Exception ex) { // Display message if exception occurs System.out.println( "host not found" ); } } } |
Output:
This article is contributed by Rishabh Mahrsee and Aparna Vadlamani. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.