Get Hostname from IP Address in Java
This section provides an example of parsing the Hostname from an IP Address provided by the user using Java.
Also, Get IP Address from Hostname in Java
The following program requires the java.net package for the provision of a Networking Class- InetAddress class. The InetAddress class is used to represent an IP address and perform DNS lookups.
The InetAddress class cannot be instantiated using its constructors. The static methods of the InetAddress class are invoked directly by referencing the class name, without the reference of an object created using a constructor.
Some of the available methods are used in the process of getting the hostname from an IP address. In the following implementation, the user provides the IP address.
The methods in the InetAddress class that are used in the following implementation are as follows:
public static InetAddress getByName(String host) throws UnknownHostException
- Returns an object of type InetAddress class.
- The object contains LocalHost IP and name.
public String getHostName()
- Returns the hostname of the IPAddress as a String type data.
|Parsing Hostname for a given IP Address|