Updated April 12, 2023
Introduction to Java InetAddress
The java InetAddress is used to specify an IP address. The IP address is a unique numerical label assign to the machine in a network. The IP address is specified in 32 bit for IPv4 and 128 bit for IPv6 address. An instance of InetAddress specifies an IP address which is a hostname, based on whether hostname resolution was performed during the creation. There are two types of addresses, Unicast and Multicast. A single interface is identifying by Unicast address, and a set of interfaces is identifying by Multicast address.
The InetAddress class in java is built in java.net.InetAddress package of java.
The InetAddress class can be used to get any host’s IP address like www.educba.com, www.google.com, and all. The commonly used IP address is IPv4 for “version 4”. Consider an example of an IP address which might look like as –
65.172.248.170
The above address contains four numbers, and each number consists of three digits separated by ‘.’ (single dot). The range for each of the four numbers is from 0 to 255.
InetAddress class member Functions
The InetAddress class does not contain any constructor but contains some function as anInetAddress class member function.
Member functions of Java InetAddress Class –
- public static InetAddressgetByAddress(String host, byte[] addr) throws UnknownHostException – This static function returns the object of InetAddress, which contains the hostname and IP address as the parameter passed. The host parameter can be an IP address in string format or a machine name like www.educba.com.
- public static InetAddressgetByName(String host) – This static function returns the object of InetAddress of the host specified. Where the parameter host is specified hostname.
- public static InetAddress[] getAllByName(String host) – This static function returns an array of InetAddress object of the host specified.
- public static InetAddressgetLoopbackAddress() – This static function returns the object of InetAddress of type loopback.
- public static InetAddressgetLocalHost() throws UnknownHostException – This static function returns the object of InetAddress of the localhost.
- public byte[] getAddress() – This function returns an IP address of InetAddress object as an array. Array store ip address in order of bytes appears as in IP address.
- public String getHostAddress() – This function returns IP address in string format.
- public boolean isReachable(int timeout)throws IOException – This function returns true if the IP address is reachable, whereas the timeout parameter specifies the time after that the call is to be abort and result in a false value.
- public boolean isReachable(NetworkInterfacenetif, intttl, int timeout) throws IOException – This function is overloaded of isReachable() function. Where netif represents the network interface to be used to check reachability, ttl represents before exiting the network the number of hops the echo packet makes, and as the timeout parameter specifies the time after that, the call is to be abort.
- public String getHostName() – This function returns the hostname of IP Address.
- public String getCanonicalHostName() – This function returns the fully qualified domain name of the IP address.
- public String toString() – This function represents an IP address in the string format.
- public boolean isAnyLocalAddress()– This function returns true if InetAddress object address is a local address.
- public boolean isLinkLocalAddress() – This function returns true if InetAddress object address is a link-local address.
- public boolean isLoopbackAddress() – This function returns true if the InetAddress object address is a loopback address.
- public boolean isMCGloabal() – This function returns true if the IP multicast address has global scope.
- public boolean isMCLinkLocal() – This function returns true if the IP multicast address has link scope.
- public boolean isMCNodeLocal() – This function returns true if IP multicast address has node scope.
- public boolean isMCOrgLoacal() – This function returns true if the IP multicast address has organization scope.
- public boolean isMCSiteLocal() – This function returns true if IP multicast address has site scope.
- public boolean isMulticastAddress() – This function returns true if the IP address is a multicast address, whose first four bits are 1110.
- public boolean isSiteLocalAddress()– This function returns true if IPaddress is a site-local address.
- public int hashCode() – This function returns IP address hashcode.
- public boolean equals(Object obj) – This function returns true if the IP address is the same as the passed IP address.
Examples for the InetAddress class in java
Next, we write the java code to understand the InetAddress class more clearly with the following example where we create an InetAddress object by using the URL and some of the function in this object which discusses above –
Example #1
Code:
import java.io.IOException;
import java.util.Arrays;
import java.net.InetAddress;
public class Demo
{
public static void main( String[] arg) throws IOException
{
InetAddress ip = InetAddress.getByName("www.educba.com");
byte addr[] = { 65, 2, 0, 1};
System.out.print("iptoString : " + ip.toString());
System.out.print("\ngetAllByName : " + ip.getAllByName("www.educba.com"));
InetAddress ips[] = InetAddress.getAllByName("www.educba.com");
System.out.println("IP Address");
for (InetAddress add:ips)
System.out.println(add.getHostAddress());
// function getByName()
System.out.print("\ngetByName : " + ip);
// function getByAddress()
System.out.print("\ngetByAddress : " +InetAddress.getByAddress(addr));
// function getLocalHost()
System.out.print("\ngetLocalHost : " +InetAddress.getLocalHost());
// function getLoopbackAddress()
System.out.print("\ngetLoopbackAddress : " +InetAddress.getLoopbackAddress());
// function getAllByName() which returns all ip addresses of google.com
System.out.print("\nGoogleip addresses : " + Arrays.toString(InetAddress.getAllByName("www.google.com")));
// function isReachable()
System.out.print("\nip address isReachable : " +ip.isReachable(50));
// function getHostname()
System.out.print("\nip address hostname :" +ip.getHostName());
// function getCanonicalHostname()
System.out.print("\nip address CanonicalHostname : " + ip.getCanonicalHostName());
}
}
Output:
Next, we write the java code to for the InetAddress class where we apply the remaining Boolean function on the InetAddress object –
Example #2
Code:
import java.net.Inet4Address;
import java.util.Arrays;
import java.net.InetAddress;
public class Demo
{
public static void main(String[] arg) throws Exception
{
InetAddress ip = Inet4Address.getByName("www.educba.com");
InetAddress ip1[] = InetAddress.getAllByName("www.educba.com");
byte addr[]={68, 5, 2, 12};
System.out.println("ip : "+ip);
System.out.print("\nip1 : "+ip1);
InetAddress ip2 = InetAddress.getByAddress(addr);
System.out.print("\nip2 : "+ip2);
System.out.print("\nAddress : " +Arrays.toString(ip.getAddress()));
System.out.print("\nHost Address : " +ip.getHostAddress());
System.out.print("\nisAnyLocalAddress : " +ip.isAnyLocalAddress());
System.out.print("\nisLinkLocalAddress : " +ip.isLinkLocalAddress());
System.out.print("\nisLoopbackAddress : " +ip.isLoopbackAddress());
System.out.print("\nisMCGlobal : " +ip.isMCGlobal());
System.out.print("\nisMCLinkLocal : " +ip.isMCLinkLocal());
System.out.print("\nisMCNodeLocal : " +ip.isMCNodeLocal());
System.out.print("\nisMCOrgLocal : " +ip.isMCOrgLocal());
System.out.print("\nisMCSiteLocal : " +ip.isMCSiteLocal());
System.out.print("\nisMulticastAddress : " +ip.isMulticastAddress());
System.out.print("\nisSiteLocalAddress : " +ip.isSiteLocalAddress());
System.out.print("\nhashCode : " +ip.hashCode());
System.out.print("\n Is ip1 == ip2 : " +ip.equals(ip2));
}
}
Output:
Conclusion
The InetAddress is a built-in class in java that is available in the java.net.InetAddress package. It is used to specify the IP address of the machine in a network. The above method, which we discussed, can be used to get more information regarding an IP address.
Recommended Articles
This is a guide to Java InetAddress. Here we discuss the InetAddress class member Functions and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –