Reverse DNS

Reverse DNS

If I can use the “InetAddress” class to acquire the IP address of a computer, is there a way to get the computer’s name from an IP address?

“InetAddress” can be used both to discover the IP address of a host and to discover the host name corresponding to an IP address. When you call “InetAddress.getByName(String host),” the host argument can be either a host name or a text representation of an IP address.

For example, run the following program with as an argument and it will print:

Name: www.sun.comIP:

Run it with as an argument and it will print the same thing (if maps to multiple IP addresses, the actual address that results when you run the program may differ).

import*;import*;public class lookup {  public static void main(String[] args) {    if(args.length != 1) {      System.err.println("lookup host");      return;    }    try {      InetAddress address;      address = InetAddress.getByName(args[0]);      System.out.println("Name: " + address.getHostName());      System.out.println("IP: " + address.getHostAddress());    } catch(IOException ioe) {      ioe.printStackTrace();    }  }}

See also  10 Best Apps to Help You Read More Books in Less Time

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist