java.net.InetAddress
About
InetAddress
is a class in the java.net
package that represents an IP address — either IPv4 or IPv6. It is used to identify hosts (computers, servers, etc.) on the Internet or a local network. We can use InetAddress
to resolve hostnames to IP addresses and vice versa, or to get information about the local machine’s IP configuration.
Why It’s Important
In networking applications, we often need to work with IP addresses — either to connect to a server, identify a client, or bind a service to a specific interface. InetAddress
provides a high-level abstraction over IP addresses, simplifying these operations.
It hides low-level byte-level IP address handling and makes tasks like DNS lookups easy using simple methods.
Key Methods
getByName(String host)
Returns the IP address of the given hostname.
getAllByName(String host)
Returns all IP addresses associated with a hostname.
getLocalHost()
Returns the IP address of the local machine.
getHostName()
Returns the hostname associated with this IP address.
getHostAddress()
Returns the textual representation of the IP address.
isReachable(int timeout)
Tests if the address is reachable within a timeout period.
equals(Object obj)
Compares two InetAddress
objects for equality.
toString()
Returns the string representation in the format: hostname / IP.
Example
import java.net.*;
public class InetAddressExample {
public static void main(String[] args) throws Exception {
InetAddress address = InetAddress.getByName("openai.com");
System.out.println("Host Name: " + address.getHostName());
System.out.println("IP Address: " + address.getHostAddress());
System.out.println("Is Reachable: " + address.isReachable(2000));
}
}
Host Name: openai.com
IP Address: 104.18.30.59
Is Reachable: true
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Main {
public static void main(String[] args) {
try {
InetAddress localHost = InetAddress.getLocalHost();
System.out.println("Local Host: " + localHost);
System.out.println("Host Name: " + localHost.getHostName());
System.out.println("Host Address: " + localHost.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
Local Host: my-machine/192.168.1.10
Host Name: my-machine
Host Address: 192.168.1.10
Last updated
Was this helpful?