Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IP Address to Hostname in Java?

Tags:

My hosts file (C:\WINDOWS\system32\drivers\etc\hosts) has a bunch of IP Address to host name mappings:

# Switches 192.168.200.254       sw-con-ctrl 192.168.201.253    sw-con-ctrl-2 192.168.201.254       sw-con-ctrl-1 # 192.168.188.1       sw-con-ctrl-ylw-1 # 192.168.189.1       sw-con-ctrl-blu 192.168.190.62        access-console  # Routers 192.168.21.1          rtr1 192.168.22.1          rtr2 

I am trying to find a way to convert from an IPAddress to the HostName programmatically through Java APIs.

Pseudocode:

IPAddress ip = new IPAddress("192.168.190.62"); String host = ip.getHost(); System.out.println(host);  //prints "access-console" 
like image 436
mainstringargs Avatar asked Jul 30 '10 13:07

mainstringargs


People also ask

Which method will display hostname?

The getHostName() method Java InetAddress returns the host name of a corresponding IP address. If this InetAddress was created with a host name, this host name will be remembered and returned else a reverse name lookup will be performed and the result will be returned based on the system configured name lookup service.

What is IP address in Java?

Java InetAddress class represents an IP address. The java. net. InetAddress class provides methods to get the IP of any host name for example www.javatpoint.com, www.google.com, www.facebook.com, etc. An IP address is represented by 32-bit or 128-bit unsigned number.


1 Answers

I tried the code from here and it works. Namely:

  InetAddress addr = InetAddress.getByName("192.168.190.62");   String host = addr.getHostName();   System.out.println(host); 
like image 91
user85509 Avatar answered Sep 28 '22 02:09

user85509