1

I am trying to get the name of computer from its local IP. This is what I've done so far, but nothing works it just returns the IP address as hostname

InetAddress addr = null;
String hostIP = "192.168.100.10";

try {
    addr = InetAddress.getByName(hostIP);
} catch (UnknownHostException e) {
    System.out.println("Host " + hostIP + " not found!");
}

String host = addr.getHostAddress();
String hostname= addr.getHostName();
String hostname2 = addr.getCanonicalHostName();

I am expecting to get DESKTOP-DWASDFW as hostname but instead i get 192.168.100.10

UPDATE:
I managed to "fix" it by adding my computer and its ip address manually into the router configuration

https://i.imgur.com/6yDK91v.png

but this isn't what im looking for as it requires manually entering every name and IP address. I want something like this app WakeOnLan, it can scan the whole network and get IP address, MAC address and hostname. This app uses arp requests, i researched a lot about how to accomplish something like that in android but found nothing that worked for me (I'm using android 4.2.2)

SenaCarbtc
  • 121
  • 3
  • 15
  • Does your network meet the requirements stated in the docs for doing the name lookup service? – Dave Newton Jul 25 '18 at 16:43
  • Checkout this answer https://stackoverflow.com/questions/3371879/ip-address-to-hostname-in-java – Saleh Mahmood Jul 25 '18 at 16:45
  • @DaveNewton i believe it does because I've used numerous android applications that scan the whole network, like WakeOnLan gives me the ip adress along with the name of the pc just by a search, zANTI also does a full network search and gives me ip adress and name, so i believe my network supports that but i have no idea how to do it – SenaCarbtc Jul 25 '18 at 16:46
  • @SalehMahmood already tried all that, the getlocaladdress() returns localhost as name but other than that everything else gives me back the IP address – SenaCarbtc Jul 25 '18 at 16:51

0 Answers0