java.net.InetAddress class
java.net.InetAddress
Direct Known Subclasses: Inet4Address, Inet6Address
This class represents an Internet Protocol (IP) address.
An IP address is either a 32-bit or 128-bit unsigned number used by IP, a lower-level protocol on which protocols like UDP and TCP are built.
Creating Objects
InetAddress.getByName("www.example.com"):
Retrieves the InetAddress object representing the IP address of "www.example.com".
InetAddress.getAllByName("www.example.com"):
Retrieves an array of InetAddress objects representing all IP addresses associated with "www.example.com".
java.net.InetAddress Methods
public byte[] getAddress()
Returns the raw IP address of this InetAddress object.
public static InetAddress[] getAllByName(String host)
Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system.
public static InetAddress getByAddress(byte[] addr)
Returns an InetAddress object given the raw IP address .
public static InetAddress getByAddress(String host, byte[] addr)
Creates an InetAddress based on the provided host name and IP address.
public static InetAddress getByName(String host)
Determines the IP address of a host, given the host's name.
public String getCanonicalHostName()
Gets the fully qualified domain name for this IP address.
public String getHostAddress()
Returns the IP address string in textual presentation.
public String getHostName()
Gets the host name for this IP address.
public static InetAddress getLocalHost()
Returns the address of the local host.
public static InetAddress getLoopbackAddress()
Returns the loopback address.
public int hashCode()
Returns a hashcode for this IP address.
public boolean isAnyLocalAddress()
Utility routine to check if the InetAddress in a wildcard address.
public boolean isLinkLocalAddress()
Utility routine to check if the InetAddress is an link local address.
public boolean isLoopbackAddress()
Utility routine to check if the InetAddress is a loopback address.
public boolean isMCGlobal()
Utility routine to check if the multicast address has global scope.
public boolean isMCLinkLocal()
Utility routine to check if the multicast address has link scope.
public boolean isMCNodeLocal()
Utility routine to check if the multicast address has node scope.
public boolean isMCOrgLocal()
Utility routine to check if the multicast address has organization scope.
public boolean isMCSiteLocal()
Utility routine to check if the multicast address has site scope.
public boolean isMulticastAddress()
Utility routine to check if the InetAddress is an IP multicast address.
public boolean isReachable(int timeout)
Test whether that address is reachable.
public boolean isReachable(NetworkInterface netif, int ttl, int timeout)
Test whether that address is reachable.
public boolean isSiteLocalAddress()
Utility routine to check if the InetAddress is a site local address.
public String toString()
Converts this IP address to a String.
java.net.InetAddress Class Example
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressExample {
public static void main(String[] args) {
try {
// Getting InetAddress instance for a domain name
InetAddress address = InetAddress.getByName("www.example.com");
// Displaying the IP address
System.out.println("IP Address: " + address.getHostAddress());
// Displaying the host name
System.out.println("Host Name: " + address.getHostName());
// Checking if the address is reachable
// Timeout in milliseconds
System.out.println("Reachable? " + address.isReachable(5000));
// Getting all IP addresses associated with a domain name
InetAddress[] addresses = InetAddress.getAllByName("www.example.com");
System.out.println("All IP Addresses for www.example.com:");
for (InetAddress addr : addresses) {
System.out.println(addr);
}
} catch (UnknownHostException e) {
System.out.println("Unable to find address for given host");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
IP Address: 93.184.216.34
Host Name: www.example.com
Reachable? true
All IP Addresses for www.example.com:
www.example.com/93.184.216.34