java.net.Inet6Address Class in Java
java.net.Inet6Address
This class represents an Internet Protocol version 6 (IPv6) address
Textual representation of IP addresses
Textual representation of IPv6 address used as input to methods takes one of the following forms:
The preferred form is x:x:x:x:x:x:x:x, where the 'x's are the hexadecimal values of the eight 16-bit pieces of the address. This is the full form. For example,
1080:0:0:0:8:800:200C:417A
Note that it is not necessary to write the leading zeros in an individual field. However, there must be at least one numeral in every field, except as described below.
Due to some methods of allocating certain styles of IPv6 addresses, it will be common for addresses to contain long strings of zero bits. In order to make writing addresses containing zero bits easier, a special syntax is available to compress the zeros. The use of "::" indicates multiple groups of 16-bits of zeros. The "::" can only appear once in an address. The "::" can also be used to compress the leading and/or trailing zeros in an address. For example,
1080::8:800:200C:417A
An alternative form that is sometimes more convenient when dealing with a mixed environment of IPv4 and IPv6 nodes is x:x:x:x:x:x:d.d.d.d, where the 'x's are the hexadecimal values of the six high-order 16-bit pieces of the address, and the 'd's are the decimal values of the four low-order 8-bit pieces of the standard IPv4 representation address, for example,
::FFFF:129.144.52.38
::129.144.52.38
where "::FFFF:d.d.d.d" and "::d.d.d.d" are, respectively, the general forms of an IPv4-mapped IPv6 address and an IPv4-compatible IPv6 address. Note that the IPv4 portion must be in the "d.d.d.d" form. The following forms are invalid:
::FFFF:d.d.d
::FFFF:d.d
::d.d.d
::d.d
The following form:
::FFFF:d
is valid, however it is an unconventional representation of the IPv4-compatible IPv6 address,
::255.255.0.d
while "::d" corresponds to the general IPv6 address "0:0:0:0:0:0:0:d".
For methods that return a textual representation as output value, the full form is used. Inet6Address will return the full form because it is unambiguous when used in combination with other textual data.
Creating Objects
InetAddress.getByName("::1"):
Retrieves the InetAddress object representing the IPv6 address "::1", which is the loopback address in IPv6.
java.net.Inet6Address Methods
public byte[] getAddress()
Returns the raw IP address of this InetAddress object.
public static Inet6Address getByAddress(String host, byte[] addr, int scope_id)
Create an Inet6Address in the exact manner of InetAddress.getByAddress(String,byte[]) except that the IPv6 scope_id is set to the given numeric value.
public static Inet6Address getByAddress(String host, byte[] addr, NetworkInterface nif)
Create an Inet6Address in the exact manner of InetAddress.getByAddress(String,byte[]) except that the IPv6 scope_id is set to the value corresponding to the given interface for the address type specified in addr.
public String getHostAddress()
Returns the IP address string in textual presentation.
public NetworkInterface getScopedInterface()
Returns the scoped interface, if this instance was created with with a scoped interface.
public int getScopeId()
Returns the numeric scopeId, if this instance is associated with an interface.
public int hashCode()
Returns a hashcode for this IP address.
boolean isIPv4CompatibleAddress() Utility routine to check if the InetAddress is an IPv4 compatible IPv6 address.public boolean isAnyLocalAddress()
Utility routine to check if the InetAddress in a wildcard address.
public boolean isLinkLocalAddress()
Utility routine to check if the InetAddress is an link local address.
public boolean isLoopbackAddress()
Utility routine to check if the InetAddress is a loopback address.
public boolean isMCGlobal()
Utility routine to check if the multicast address has global scope.
public boolean isMCLinkLocal()
Utility routine to check if the multicast address has link scope.
public boolean isMCNodeLocal()
Utility routine to check if the multicast address has node scope.
public boolean isMCOrgLocal()
Utility routine to check if the multicast address has organization scope.
public boolean isMCSiteLocal()
Utility routine to check if the multicast address has site scope.
public boolean isMulticastAddress()
Utility routine to check if the InetAddress is an IP multicast address.
public boolean isSiteLocalAddress()
Utility routine to check if the InetAddress is a site local address.
java.net.Inet6Address Class Example
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class Inet6AddressExample {
public static void main(String[] args) {
try {
// Get Inet6Address instance representing localhost
Inet6Address localhost = (Inet6Address) InetAddress.getByName("::1");
// Displaying the IP address
System.out.println("Localhost IP Address: " + localhost.getHostAddress());
// Displaying the host name
System.out.println("Localhost Host Name: " + localhost.getHostName());
// Getting all IP addresses associated with the localhost
InetAddress[] allLocal = InetAddress.getAllByName("localhost");
System.out.println("All IP Addresses for localhost:");
for (InetAddress addr : allLocal) {
System.out.println(addr);
}
// Displaying all IPv6 addresses associated with all network interfaces
System.out.println("\nIPv6 Addresses of all Network Interfaces:");
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface netInterface = networkInterfaces.nextElement();
System.out.println("Interface: " + netInterface.getName());
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
if (address instanceof Inet6Address) {
System.out.println("IPv6 Address: " + address.getHostAddress());
}
}
System.out.println();
}
} catch (SocketException e) {
System.out.println("SocketException occurred while retrieving network interfaces.");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Localhost IP Address: 0:0:0:0:0:0:0:1
Localhost Host Name: localhost
All IP Addresses for localhost:
localhost/0:0:0:0:0:0:0:1
127.0.0.1
IPv6 Addresses of all Network Interfaces:
Interface: lo
IPv6 Address: 0:0:0:0:0:0:0:1
Interface: eth0
IPv6 Address: fe80:0:0:0:250:56ff:fe8d:87c0