Last active
April 10, 2019 06:58
-
-
Save roshanadh/4172bc952300ca57218d32aed4d1815e to your computer and use it in GitHub Desktop.
Get Hostname from a given IP address in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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