Retrieve the hostname by IP Address in Java

Description

The following code shows how to retrieve the hostname by IP Address.

Example


//from  ww w  .  j  a  va  2s . c  om
import java.net.InetAddress;

public class Main {
  public static void main(String[] argv) throws Exception {
    
    byte[] ipAddr = new byte[] { (byte)173, (byte)194, 33, 115 };
    InetAddress addr = InetAddress.getByAddress(ipAddr);
    // Get the host name
    String hostname = addr.getHostName();
    System.out.println(hostname);
    // Get canonical host name
    String canonicalhostname = addr.getCanonicalHostName();
    System.out.println(canonicalhostname);
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Network »




NetworkInterface
URI
URL
HTTP
HTTP Read
IP
Socket
UDP
URL Encode