4

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();
    }
Umar Iqbal
  • 47
  • 1
  • 1
  • 8
  • what entries do you have in your hosts file – Shreyas Sarvothama Oct 26 '16 at 16:07
  • Look at this it will solve your problem: http://stackoverflow.com/questions/21521844/how-to-resolve-network-host-names-from-ip-address – Shreyas Sarvothama Oct 26 '16 at 16:08
  • Shreyas , i add this InetAddress.getByName("192.168.1.2").getHostName() after opening your link but still give me same ip 192.168.1.2 – Umar Iqbal Oct 26 '16 at 17:10
  • no, they give me the same ip that i passed in getByName method. – Umar Iqbal Oct 26 '16 at 17:24
  • The hostname will be given by DNS Server.. to which DNS server are you connected to ? – Shreyas Sarvothama Oct 26 '16 at 17:26
  • i want to get hostname and ip address of all connected devices in my LAN network. If you know the exact code please post here. – Umar Iqbal Oct 26 '16 at 17:26
  • the code is right, you have something called hosts file in windows/unix you need to assign a name for those ip addresses there.. do you know where to look for hosts file? – Shreyas Sarvothama Oct 26 '16 at 17:28
  • no. i don't know , please tell me , i need your help. – Umar Iqbal Oct 26 '16 at 17:34
  • goto this link, follow the steps they have done.. until you can open the hosts file... once you open the hosts file.. just add in new line 192.168.1.1 Umar then re-run the code you will see Umar – Shreyas Sarvothama Oct 26 '16 at 17:36
  • where is the link? – Umar Iqbal Oct 26 '16 at 17:39
  • Sorry here it is: http://www.howtogeek.com/140576/how-to-edit-the-hosts-file-on-android-and-block-web-sites/ – Shreyas Sarvothama Oct 26 '16 at 17:41
  • sorry to say that i think you have not get my point. Let suppose i connect to a router and my local ip address is 192.168.1.2 ,and 5 other people are also connect to that router and let suppose there ip address is 3,4,5,6 according to my ip address, i just want to get these five people ip addresses and there names. – Umar Iqbal Oct 26 '16 at 17:46
  • the link that you send it give me website's ips – Umar Iqbal Oct 26 '16 at 17:46
  • if you have access to windows, goto C:\Windows\System32\drivers\etc and open hosts file. Add your ip address there give a space and then give a name. After that copy paste your code in eclipse and run.. you will understand what i am talking about – Shreyas Sarvothama Oct 26 '16 at 18:07

1 Answers1

5

Use .getHostname()

InetAddress addr = InetAddress.getByName("192.168.1.1");
String host = addr.getHostName();
System.out.println(host);
SH151
  • 136
  • 7