Thursday, April 22, 2010

Get Host Name by IP Address using Java

Posted on 3:01 PM by Kalum Umesh

To get a host or domain name, you will need to get an instance of InetAddress. In this case, we will provide a string representation of an IP address. Once we have an instance of InetAddresss we can request the host or domain name using the getHostName() method.

NOTE: The getHostName() method may not be able to resolve the host name for the specified IP address. If this occurs, getHostName() will return the IP address.


try { 
         InetAddress addr = InetAddress.getLocalHost(); 
         // Get IP Address 
        byte[] ipAddr = addr.getAddress();
        // Get hostname 
        String hostname = addr.getHostName();
catch (UnknownHostException e) { }

No Response to "Get Host Name by IP Address using Java"

Leave A Reply

 
Free Website Hosting