You are here: Home / Topics / Program to Display host name by IP address in Java

Program to Display host name by IP address in Java

Filed under: Java on 2023-10-25 06:48:30

// Program to Display host name by IP address.

import java.net.*;

class HostName
{
public static void main( String args[ ] ) 
{
 // byte ad[ ] = { (byte)192, (byte)168, 1, 2 };
 
 try
 {
  // InetAddress ip = InetAddress.getByAddress( ad );
  InetAddress ip = InetAddress.getByAddress( new byte[ ] { (byte)192, (byte)168, 1, 2 } );
  System.out.println("  " + ip.getHostName());
 }
 catch( UnknownHostException ue )
 {
  System.out.println(" Can't find given host.");
 }  
}
}


Output:

nils

About Author:
J
Java     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.