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.
The InetAddress class does not contain any constructor but contains some function as anInetAddress class member function.
Member functions of Java InetAddress Class –
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 –
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 –
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:
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!