How to get Host name of ip address in android?
10,057
Use .getHostname()
InetAddress addr = InetAddress.getByName("192.168.1.1");
String host = addr.getHostName();
System.out.println(host);
Author by
Umar Iqbal
Updated on June 04, 2022Comments
-
Umar Iqbal almost 2 years
I want to make a small android app that get ip addresses and host name from LAN network that are connected. I have a code that run great to get ip address in a LAN network which is connected but I don't know how to get host name of their ip addresses. Where I need to changing in code. Sorry for bad English.
Here is my code for getting ip address in lan network
String connections = ""; InetAddress host; try { host = InetAddress.getByName("192.168.1.1"); byte[] ip = host.getAddress(); for(int i = 1; i <= 254; i++) { ip[3] = (byte) i; InetAddress address = InetAddress.getByAddress(ip); if(address.isReachable(100)) { System.out.println(address + " machine is turned on and can be pinged "+address.getCanonicalHostName()); connections+= address+"\n"; } else if(!address.getHostAddress().equals(address.getHostName())) { System.out.println(address + " machine is known in a DNS lookup"); System.out.println(address.getHostAddress()+"host Name:"+ address.getHostName()); } } tv.setText(connections); } catch(UnknownHostException e1) { e1.printStackTrace(); } catch(IOException e) { e.printStackTrace(); }