Get IP Address from Hostname in Java
This section provides an example of parsing the IP Address from a host name provided by the user using Java.
Also, Get Hostname from IP Address 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 IP address from a hostname. In the following implementation, the user provides the hostname.
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 getHostAddress()
- Returns the IP address as a String type data.
|Parsing IP address for a given Hostname|