Web Analytics
Get IP Address from Hostname in Java -->

Get IP Address from Hostname in Java

Learn how to get IP address from a Hostname provided by the user, with Java. Parse the hostname and retrieve the IP address using InetAddress class.

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
Parsing IP address for a given Hostname

Implementation

You may like these posts

  1. To insert a code use <i rel="pre">code_here</i>
  2. To insert a quote use <b rel="quote">your_qoute</b>
  3. To insert a picture use <i rel="image">url_image_here</i>