Skip to content

Instantly share code, notes, and snippets.

@roshanadh
Last active April 10, 2019 06:58
Show Gist options
  • Save roshanadh/4172bc952300ca57218d32aed4d1815e to your computer and use it in GitHub Desktop.
Save roshanadh/4172bc952300ca57218d32aed4d1815e to your computer and use it in GitHub Desktop.
Get Hostname from a given IP address in Java
import java.net.*;
import java.io.*;
public class IPAddressDemo
{
public static void main(String[] args) throws IOException
{
System.out.println("Type the IP Address: ");
BufferedReader readIP=new BufferedReader(new InputStreamReader(System.in));
String ipaddress=readIP.readLine();
try{
InetAddress host= InetAddress.getByName(ipaddress);
System.out.println("The hostname for "+ipaddress+" is: "+host.getHostName());
}
catch(UnknownHostException e)
{
System.out.println("Host name not found for given IP Address");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment