Performing an nslookup in Java



Introduction

In Java it’s simply done by the InetAddress Object as follows:

try {
    // Get hostname by textual representation of
    // IP address
    InetAddress addr = 
    InetAddress.getByName("127.0.0.1");

    // Get hostname by a byte array containing
       the IP address
    byte[] ipAddr = new byte[]{127, 0, 0, 1};
    addr = InetAddress.getByAddress(ipAddr);

    // Get the host name
    String hostname = addr.getHostName();

    // Get canonical host name
    String hostnameCanonical = addr.getCanonicalHostName();
} catch (UnknownHostException e) {
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.