Understanding the Java InetAddress Class
Explore the Java InetAddress class, which is essential for handling IP addresses in network programming. This guide covers how to retrieve IP addresses for hostnames and discusses caching for hostname resolutions, along with the two versions of IP addresses.
Java InetAddress Class
The Java InetAddress class represents an IP address. It provides methods to get the IP of any hostname, e.g., www.tutorialsarena.com, www.google.com. It uses a cache to store host name resolutions.
IP Address
An IP address identifies a specific resource on the network using a numerical representation. Most networks combine IP with TCP (Transmission Control Protocol). There are two versions of IP address:
- IPv4: The primary Internet protocol, using a 32-bit addressing scheme. It's a connectionless protocol, uses less memory, and supports video conferencing and libraries.
- IPv6: The latest Internet protocol, using a 128-bit address space, supports both stateful and stateless configurations, QoS, and has hierarchical addressing and routing infrastructure.
TCP/IP Protocol
TCP/IP is a communication protocol model used to connect devices over a network via the internet. The two main protocols are:
- TCP (Transmission Control Protocol): Creates a communication channel across the network and helps in transmission of packets.
- IP (Internet Protocol): Provides the address to nodes connected on the internet and ensures correct message forwarding.
Java InetAddress Class Methods
Method | Description |
---|---|
public static InetAddress getByName(String host) throws UnknownHostException | Returns the instance of InetAddress containing the host's IP and name. |
public static InetAddress getLocalHost() throws UnknownHostException | Returns the instance of InetAddress containing the local host's name and address. |
public String getHostName() | Returns the host name of the IP address. |
public String getHostAddress() | Returns the IP address in string format. |
Example of Java InetAddress Class
Get the IP address of www.tutorialsarena.com:
Syntax
import java.io.*;
import java.net.*;
public class InetDemo {
public static void main(String[] args) {
try {
InetAddress ip = InetAddress.getByName("www.tutorialsarena.com");
System.out.println("Host Name: " + ip.getHostName());
System.out.println("IP Address: " + ip.getHostAddress());
} catch (Exception e) {
System.out.println(e);
}
}
}
Output
Host Name: www.tutorialsarena.com
IP Address: 172.67.196.82
Program to Demonstrate Methods of InetAddress Class
Syntax
import java.net.Inet4Address;
import java.util.Arrays;
import java.net.InetAddress;
public class InetDemo2 {
public static void main(String[] arg) throws Exception {
InetAddress ip = Inet4Address.getByName("www.tutorialsarena.com");
InetAddress ip1[] = InetAddress.getAllByName("www.tutorialsarena.com");
byte addr[] = {72, 3, 2, 12};
System.out.println("ip : " + ip);
System.out.println("ip1 : " + Arrays.toString(ip1));
InetAddress ip2 = InetAddress.getByAddress(addr);
System.out.println("ip2 : " + ip2);
System.out.println("Address : " + Arrays.toString(ip.getAddress()));
System.out.println("Host Address : " + ip.getHostAddress());
System.out.println("isAnyLocalAddress : " + ip.isAnyLocalAddress());
System.out.println("isLinkLocalAddress : " + ip.isLinkLocalAddress());
System.out.println("isLoopbackAddress : " + ip.isLoopbackAddress());
System.out.println("isMCGlobal : " + ip.isMCGlobal());
System.out.println("isMCLinkLocal : " + ip.isMCLinkLocal());
System.out.println("isMCNodeLocal : " + ip.isMCNodeLocal());
System.out.println("isMCOrgLocal : " + ip.isMCOrgLocal());
System.out.println("isMCSiteLocal : " + ip.isMCSiteLocal());
System.out.println("isMulticastAddress : " + ip.isMulticastAddress());
System.out.println("isSiteLocalAddress : " + ip.isSiteLocalAddress());
System.out.println("hashCode : " + ip.hashCode());
System.out.println("Is ip1 == ip2 : " + ip.equals(ip2));
}
}
Output
ip : www.tutorialsarena.com/172.67.196.82
ip1 : [www.tutorialsarena.com/172.67.196.82, www.tutorialsarena.com/172.67.196.82]
ip2 : /72.3.2.12
Address : [172, 67, 196, 82]
Host Address : 172.67.196.82
isAnyLocalAddress : false
isLinkLocalAddress : false
isLoopbackAddress : false
isMCGlobal : false
isMCLinkLocal : false
isMCNodeLocal : false
isMCOrgLocal : false
isMCSiteLocal : false
isMulticastAddress : false
isSiteLocalAddress : false
hashCode : -2115798592
Is ip1 == ip2 : false