Saturday, December 28, 2024
Google search engine
HomeLanguagesJavajava.net.InetAddress Class in Java

java.net.InetAddress Class in Java

public class InetAddress extends Object implements Serializable:

The java.net.InetAddress class provides methods to get the IP address of any hostname. An IP address is represented by 32-bit or 128-bit unsigned number. InetAddress can handle both IPv4 and IPv6 addresses. 

There are 2 types of addresses :

  1. Unicast — An identifier for a single interface.
  2. Multicast — An identifier for a set of interfaces.

InetAddress – Factory Methods :

The InetAddress class is used to encapsulate both, the numerical IP address and the domain name for that address. The InetAddress class has no visible constructors. The InetAddress class has the inability to create objects directly, hence factory methods are used for the purpose. Factory Methods are static methods in a class that return an object of that class.

There are 5 factory methods available in InetAddress class – 

Method  Description
public static InetAddress getLocalHost() throws UnknownHostException This method returns the instance of InetAddress containing the local hostname and address.
public static InetAddress getByName( String host ) throws UnknownHostException This method returns the instance of InetAddress containing IP and Host name of host represented by host argument.
public static InetAddress[] getAllByName( String hostName ) throws UnknownHostException This method returns the array of the instance of InetAddress class which contains IP addresses.
public static InetAddress getByAddress( byte IPAddress[] ) throws UnknownHostException This method returns an InetAddress object created from the raw IP address.
public static InetAddress getByAddress( String hostName, byte IPAddress[] ) throws UnknownHostException This method creates and returns an InetAddress based on the provided hostname and IP address.

Below is the Java implementation of InetAddress class to demonstrate the use of factory methods – 

Java




import java.io.*;
import java.net.*;
import java.util.*;
 
class GFG {
    public static void main(String[] args)
        throws UnknownHostException
    {
        // To get and print InetAddress of Local Host
        InetAddress address1 = InetAddress.getLocalHost();
        System.out.println("InetAddress of Local Host : "
                           + address1);
 
        // To get and print InetAddress of Named Host
        InetAddress address2
            = InetAddress.getByName("45.22.30.39");
        System.out.println("InetAddress of Named Host : "
                           + address2);
 
        // To get and print ALL InetAddresses of Named Host
        InetAddress address3[]
            = InetAddress.getAllByName("172.19.25.29");
        for (int i = 0; i < address3.length; i++) {
            System.out.println(
                "ALL InetAddresses of Named Host : "
                + address3[i]);
        }
 
        // To get and print InetAddresses of
        // Host with specified IP Address
        byte IPAddress[] = { 125, 0, 0, 1 };
        InetAddress address4
            = InetAddress.getByAddress(IPAddress);
        System.out.println(
            "InetAddresses of Host with specified IP Address : "
            + address4);
 
        // To get and print InetAddresses of Host
        // with specified IP Address and hostname
        byte[] IPAddress2
            = { 105, 22, (byte)223, (byte)186 };
        InetAddress address5 = InetAddress.getByAddress(
            "gfg.com", IPAddress2);
        System.out.println(
            "InetAddresses of Host with specified IP Address and hostname : "
            + address5);
    }
}


Output

InetAddress of Local Host : localhost/127.0.0.1
InetAddress of Named Host : /45.22.30.39
ALL InetAddresses of Named Host : /172.19.25.29
InetAddresses of Host with specified IP Address : /125.0.0.1
InetAddresses of Host with specified IP Address and hostname : gfg.com/105.22.223.186

InetAddress — Instance Methods :

InetAddress class has plenty of instance methods that can be called using the object. The instance methods are – 

Method                            Description
equals(Object obj) This function compares this object against the specified object.
getAddress() This method returns the raw IP address of this InetAddress object, in bytes.
getCanonicalHostName() This method returns the fully qualified domain name for this IP address.
 getHostAddress() This method gets the IP address in string form.
 getHostName() This method returns the host name for this IP address.
 hashCode() This method gets a hashcode for this IP address.
isAnyLocalAddress() This method utility routine to check if the InetAddress is an unpredictable address.
 isLinkLocalAddress() This method utility routine to check if the address is not linked to local unicast address.
isLoopbackAddress() This method used to check if the InetAddress represents a loopback address.
 isMCGlobal() This method utility routine check if this address has a multicast address of global scope.
 isMCLinkLocal() This method utility routine check if this address has a multicast address of link-local scope.
 isMCNodeLocal() This method utility routine check if the multicast address has node scope.
 isMCOrgLocal() This method utility routine check if the multicast address has an organization scope.
 isMCSiteLocal() This method utility routine check if the multicast address has site scope.
 isMulticastAddress() This method checks whether the site has multiple servers.
 isReachable(int timeout) This method tests whether that address is reachable.
 isReachable(NetworkInterface netif, int ttl, int timeout) This method tests whether that address is reachable.
 isSiteLocalAddress() This method utility routine check if the InetAddress is a site-local address.
 toString() This method converts and returns an IP address in string form.

Below is the Java implementation of InetAddress class to demonstrate the use of instance methods – 

Java




import java.io.*;
import java.net.*;
import java.util.*;
 
class GFG {
    public static void main(String[] args)
        throws UnknownHostException
    {
 
        InetAddress address1
            = InetAddress.getByName("45.22.30.39");
        InetAddress address2
            = InetAddress.getByName("45.22.30.39");
        InetAddress address3
            = InetAddress.getByName("172.19.25.29");
 
        // true, as clearly seen above
        System.out.println(
            "Is Address-1 equals to Address-2? : "
            + address1.equals(address2));
        // false
        System.out.println(
            "Is Address-1 equals to Address-3? : "
            + address1.equals(address3));
 
        // returns IP address
        System.out.println("IP Address : "
                           + address1.getHostAddress());
        // returns host name,
        // which is same as IP
        // address in this case
        System.out.println(
            "Host Name for this IP Address : "
            + address1.getHostName());
 
        // returns address in bytes
        System.out.println("IP Address in bytes : "
                           + address1.getAddress());
 
        // false, as the given site
        //  has only one server
        System.out.println("Is this Address Multicast? : "
                           + address1.isMulticastAddress());
 
        System.out.println("Address in string form : "
                           + address1.toString());
 
        // returns fully qualified
        // domain name for this IP address.
        System.out.println(
            "Fully qualified domain name for this IP address : "
            + address1.getCanonicalHostName());
 
        // hashcode for this IP address.
        System.out.println("Hashcode for this IP address : "
                           + address1.hashCode());
 
        // to check if the InetAddress is
        // an unpredictable address..
        System.out.println(
            "Is the InetAddress an unpredictable address? : "
            + address1.isAnyLocalAddress());
    }
}


Output

Is Address-1 equals to Address-2? : true
Is Address-1 equals to Address-3? : false
IP Address : 45.22.30.39
Host Name for this IP Address : 45.22.30.39
IP Address in bytes : [B@579bb367
Is this Address Multicast? : false
Address in string form : 45.22.30.39/45.22.30.39
Fully qualified domain name for this IP address : 45.22.30.39
Hashcode for this IP address : 756424231
Is the InetAddress an unpredictable address? : false
RELATED ARTICLES

Most Popular

Recent Comments