Home  >  Article  >  Java  >  Java InetAddress

Java InetAddress

PHPz
PHPzOriginal
2024-08-30 15:39:39860browse

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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

  1. 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.
  2. public static InetAddressgetByName(String host) – This static function returns the object of InetAddress of the host specified. Where the parameter host is specified hostname.
  3. public static InetAddress[] getAllByName(String host) – This static function returns an array of InetAddress object of the host specified.
  4. public static InetAddressgetLoopbackAddress() – This static function returns the object of InetAddress of type loopback.
  5. public static InetAddressgetLocalHost() throws UnknownHostException – This static function returns the object of InetAddress of the localhost.
  6. 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.
  7. public String getHostAddress() – This function returns IP address in string format.
  8. 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.
  9. 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.
  10. public String getHostName() – This function returns the hostname of IP Address.
  11. public String getCanonicalHostName() – This function returns the fully qualified domain name of the IP address.
  12. public String toString() – This function represents an IP address in the string format.
  13. public boolean isAnyLocalAddress()– This function returns true if InetAddress object address is a local address.
  14. public boolean isLinkLocalAddress() – This function returns true if InetAddress object address is a link-local address.
  15. public boolean isLoopbackAddress() – This function returns true if the InetAddress object address is a loopback address.
  16. public boolean isMCGloabal() – This function returns true if the IP multicast address has global scope.
  17. public boolean isMCLinkLocal() – This function returns true if the IP multicast address has link scope.
  18. public boolean isMCNodeLocal() – This function returns true if IP multicast address has node scope.
  19. public boolean isMCOrgLoacal() – This function returns true if the IP multicast address has organization scope.
  20. public boolean isMCSiteLocal() – This function returns true if IP multicast address has site scope.
  21. public boolean isMulticastAddress() – This function returns true if the IP address is a multicast address, whose first four bits are 1110.
  22. public boolean isSiteLocalAddress()– This function returns true if IPaddress is a site-local address.
  23. public int hashCode() – This function returns IP address hashcode.
  24. 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:

Java InetAddress

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:

Java InetAddress

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.

The above is the detailed content of Java InetAddress. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Java KeyStoreNext article:Java KeyStore