Java.net.Inet6Address class in Java

This class represents IPv6 address and extends the InetAddress class. Methods of this class provide facility to represent and interpret IPv6 addresses.
Methods of this class takes input in the following formats:

  1. x:x:x:x:x:x:x:x –This is the general form of IPv6 address where each x can be replaced with a 16 bit hexadecimal value of the address. Note that there must be a value in place of every ‘x’ when using this format. For example,
    4B0C:0:0:0:880C:99A8:4B0:4411
  2. When the address contains multiple set of 8 bits as ‘0’, a special format can be used to compress the address. In such cases ‘::’ is replaced in place of 0’s to make the address shorter. For example, the address in previous example can be written as-
    4B0C::880C:99A8:4B0:4411
  3. x:x:x:x:x:x:d.d.d.d –A third format is used when hybrid addressing(IPv6 + IPv4) has to be taken care of. In such cases the first 12 bytes are used for IPv6 addressing and remaining 4 bytes are used for IPv4 addressing. For example,
    F334::40CB:152.16.24.142
  4. ::FFFF:d.d.d.d – This type of addressing is known as IPv4-mapped addressing. It is used to aid in the deployment of IPv6 addressing. It allows the use of same structure and socket to communicate via both IPv6 and IPv4 connected network. First 80 bits are filled with 0’s represented by ‘::’. Next 32 bits are all ‘1’ and remaining 32 bits represent the IPv4 address. For example,
    ::FFFF:152.16.24.123

Methods :

  1. getByAddress(String host, byte[] addr, int scope_id) : This is used to create an Inet6Address object by setting a IPv6 scope id to the given value. Object returned is similar to as created by InetAddress.getByAddress(String, byte[]) with additional info about scope id.
    Syntax :public static Inet6Address getByAddress(String host,
                            byte[] addr,
                            int scope_id)
                                     throws UnknownHostException
    Parameters :
    host : host
    addr : raw ip address in network order
    scope_id : scope id of the address
    Throws :
    UnknownHostException : if IP address is of illegal length
  2. getByAddress(String host, byte[] addr, NetworkInterface nif): An overloaded method getByAddress() can be used to specify the network interface to be used with the address. In this case, the scope id corresponding to network interface is used as scope id.
    Syntax :public static Inet6Address getByAddress(String host,
                            byte[] addr,
                            NetworkInterface nif)
                                     throws UnknownHostException
    Parameters :
    host : host
    addr : raw ip address in network order
    nif : network interface to be associated with this address
    Throws :
    UnknownHostException : if IP address is of illegal length
  3. getScopeId() : Returns the scope id associated with this address or 0 if none set.
    Syntax : public int getScopeId()
  4. getScopedInterface() : Returns the network interface associated with this address or null if none set.
    Syntax : public NetworkInterface getScopedInterface()
  5. getAddress() : returns raw IP address of this InetAddress object as an array. The order in which bytes appear in array are same as in IP address i.e. getAddress[0] will contain highest order byte.
    Syntax : public byte[] getAddress()
  6. getHostAddress() : returns IP address in textual form.
    Syntax :public String getHostAddress()
  7. isAnyLocalAddress() : returns true if this address represents a local address.
    Syntax :public boolean isAnyLocalAddress()
  8. isLinkLocalAddress() : returns true if this address is a link local address.
    Syntax :public boolean isLinkLocalAddress()
  9. isLoopbackAddress() : returns true if this address is a loopback address.
    Syntax :public boolean isLoopbackAddress()
  10. isMCGlobal() : returns true if this multicast address has global scope.
    Syntax :public boolean isMCGloabal()
  11. isMCLinkLocal() : returns true if this multicast address has link scope.
    Syntax :public boolean isMCLinkLocal()
  12. isMCNodeLocal() : returns true if this multicast address has node scope.
    Syntax :public boolean isMCNodeLocal()
  13. isMCOrgLocal() : returns true if this multicast address has organisation scope.
    Syntax :public boolean isMCOrgLoacal()
  14. isMCSiteLocal() : returns true if this multicast address has site scope.
    Syntax :public boolean isMCSiteLocal()
  15. isMulticastAddress() : returns true if this address is an IP multicast address. Multicast addresses have 1110 as their first 4 bits.
    Syntax :public boolean isMulticastAddress()
  16. isSiteLocalAddress(): returns true if this address is a site local address.
    Syntax :public boolean isSiteLocalAddress()()
  17. hashCode() : returns the hashcode associated with this address object.
    Syntax : public int hashCode()
  18. equals() : returns true if this ip address is same as that of the object specified. Equals() method don’t consider host names while comparing and only consider IP address associated.
    Syntax : public boolean equals(Object obj)
    Parameters :
    obj : object to compare with

Java Implementation :


//Java program to illustrate various
//Inet6Address class methods
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

public class inet6add 
{

    public static void main(String[] args) throws UnknownHostException 
    {

        String host = "localhost";
        byte add[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };

        //getByAddress() method
        Inet6Address ip1 = Inet6Address.getByAddress(host, add, 5);
        Inet6Address ip2 = Inet6Address.getByAddress(null, add, 6);
        
        // Following methods checks the property of the thus created object.
        // getscopeId() method
        System.out.println("Scope Id : " + ip1.getScopeId());

        // getScopedInterface() method
        System.out.println("Scoped Interface : " + ip1.getScopedInterface());

        // 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 :

Scope Id : 5
Scoped Interface : null
Address : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
Host Address : 0:0:0:0:0:0:0:1%5
isAnyLocalAddress : false
isLinkLocalAddress : false
isLoopbackAddress : true
isMCGlobal : false
isMCLinkLocal : false
isMCNodeLocal : false
isMCOrgLocal : false
isMCSiteLocal : false
isMulticastAddress : false
isSiteLocalAddress : false
hashCode : 1
ip1==ip2 : true

This article is contributed by Rishabh Mahrsee. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks 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.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.