In Java, InetAddress
is part of the java.net
package and is used to handle IP addresses, both IPv4 and IPv6. It provides methods to resolve hostnames to IP addresses and vice versa, as well as to work with local and remote host information. Here’s a detailed explanation of how to work with InetAddress
to resolve IP addresses:
Basic Workflow with InetAddress
- Import the Necessary Package
import java.net.InetAddress;
import java.net.UnknownHostException;
- Resolve a Hostname to an IP Address Use
InetAddress.getByName(String hostname)
to find the IP address of a given hostname:
try {
InetAddress address = InetAddress.getByName("www.example.com");
System.out.println("Host Name: " + address.getHostName());
System.out.println("IP Address: " + address.getHostAddress());
} catch (UnknownHostException e) {
System.err.println("Host not found: " + e.getMessage());
}
- Get the Local Host Address Use
InetAddress.getLocalHost()
to retrieve the IP address of your local machine:
try {
InetAddress localHost = InetAddress.getLocalHost();
System.out.println("Local Host Name: " + localHost.getHostName());
System.out.println("Local IP Address: " + localHost.getHostAddress());
} catch (UnknownHostException e) {
System.err.println("Unable to get local host address: " + e.getMessage());
}
- Resolve All IP Addresses for a Hostname Some hostnames might resolve to multiple IP addresses (e.g., websites using load balancing). Use
InetAddress.getAllByName(String hostname)
to get all associated IPs:
try {
InetAddress[] addresses = InetAddress.getAllByName("www.google.com");
for (InetAddress address : addresses) {
System.out.println("IP Address: " + address.getHostAddress());
}
} catch (UnknownHostException e) {
System.err.println("Host not found: " + e.getMessage());
}
- Work with the Loopback Address You can retrieve and work with the default loopback address (
127.0.0.1
or::1
):
InetAddress loopback = InetAddress.getLoopbackAddress();
System.out.println("Loopback Address: " + loopback.getHostAddress());
- Check Reachability Use
InetAddress.isReachable(int timeout)
to check if an address is reachable (via ICMP ping-like mechanism):
try {
InetAddress address = InetAddress.getByName("www.example.com");
if (address.isReachable(5000)) { // Timeout in milliseconds
System.out.println(address + " is reachable.");
} else {
System.out.println(address + " is not reachable.");
}
} catch (Exception e) {
System.err.println("Error checking reachability: " + e.getMessage());
}
- Create an InetAddress from an IP String If you already have an IP address in string form and want to create an
InetAddress
object:
try {
InetAddress address = InetAddress.getByName("192.168.0.1");
System.out.println("Host Name: " + address.getHostName());
System.out.println("IP Address: " + address.getHostAddress());
} catch (UnknownHostException e) {
System.err.println("Invalid IP address: " + e.getMessage());
}
Useful Methods in InetAddress
Method | Description |
---|---|
getByName(String host) |
Returns InetAddress for the given hostname or IP address. |
getAllByName(String host) |
Returns all InetAddress objects for the given hostname. |
getLocalHost() |
Fetches the local machine’s InetAddress . |
getLoopbackAddress() |
Returns the loopback address (e.g., 127.0.0.1 ). |
getHostName() |
Returns the hostname for the IP address. |
getHostAddress() |
Gets the IP address as a string. |
isReachable(int timeout) |
Checks if the address is reachable within a given timeout. |
equals(Object obj) |
Compares two InetAddress objects. |
hashCode() |
Generates the hash code for the InetAddress instance. |
Common Use-Case Examples
Example 1: Resolving a Hostname
try {
InetAddress address = InetAddress.getByName("www.example.com");
System.out.println("IP Address: " + address.getHostAddress());
} catch (UnknownHostException e) {
System.err.println("Unable to resolve host: " + e.getMessage());
}
Example 2: Listing All IPs for a Domain
try {
InetAddress[] addresses = InetAddress.getAllByName("www.google.com");
for (InetAddress addr : addresses) {
System.out.println(addr);
}
} catch (UnknownHostException e) {
System.err.println("Unable to resolve host: " + e.getMessage());
}
Example 3: Checking Local Host Information
try {
InetAddress localHost = InetAddress.getLocalHost();
System.out.println("HostName: " + localHost.getHostName());
System.out.println("IP Address: " + localHost.getHostAddress());
} catch (UnknownHostException e) {
System.err.println("Error: " + e.getMessage());
}
Notes:
- In modern applications, DNS caching and network configurations can affect resolution.
InetAddress.getByName
internally resolves the hostname using DNS.- For non-blocking or asynchronous networking, consider Java’s
java.nio
package.
With these explanations and examples, you should be able to handle IP address resolution reliably using InetAddress
.
Latest posts by Wayan (see all)
- How do I secure servlets with declarative security in web.xml - April 24, 2025
- How do I handle file uploads using Jakarta Servlet 6.0+? - April 23, 2025
- How do I serve static files through a Jakarta Servlet? - April 23, 2025