Get the name and IP of devices on a Wifi network

7.2k views Asked by At

I know this question has been asked here but it didn't get answered.

I'm writing a simple Java Swing application in which I want to show the name and IP address of each and every device that is connected to my wireless network.

I want to show this list in a JFrame. I searched a lot on the web but couldn't find a way to achieve this. Please help me out Java masters!

Thanks in advance!

2

There are 2 answers

2
Matthew Meacham On

I found this code after looking a little bit. It works, but it is slow, and probably not the best way to do it, but it works.

import java.io.IOException;
import java.net.InetAddress;

public class NetworkPing {

    /**
     * JavaProgrammingForums.com
     */
    public static void main(String[] args) throws IOException {

        InetAddress localhost = InetAddress.getLocalHost();
        // this code assumes IPv4 is used
        byte[] ip = localhost.getAddress();

        for (int i = 1; i <= 254; i++)
        {
            ip[3] = (byte)i;
            InetAddress address = InetAddress.getByAddress(ip);
        if (address.isReachable(1000))
        {
            System.out.println(address + " machine is turned on and can be pinged");
        }
        else if (!address.getHostAddress().equals(address.getHostName()))
        {
            System.out.println(address + " machine is known in a DNS lookup");
        }
        else
        {
            System.out.println(address + " the host address and host name are equal, meaning the host name could not be resolved");
        }
        }

    }
}

Couple things to note, address.getHostAddress() returns the 192.168.0.xxx and address.getHostName() returns the name of the device like "Kevins-PC"

It's a pretty simple piece of code, but I'll walk through it real fast.

It starts off by getting your localhost IP address (which on a normal household network would be 192.168.0.xxx) and it stores that in a byte[] so it looks something like {192, 168, 0, xxx}. Then it creates a for loop starting at 1 and going to 254 (because this code assumes a /24 subnet mask (255.255.255.0) but if its running a different subnet mask then it might not be 1-254). Then in the for loop it sets the third index of the ip to i. It then creates an InetAddress from that address. Then it tries to reach it in 1000 milliseconds (1 second), and if it succeeds then it prints the address and says its reachable. Else if the machine host address (the 192.168.0.xxx) does not equal the host name (like the name of your computer like Kevins-PC), then it says that the machine is known in a DNS lookup meaning it is found in a DNS lookup but it wasnt reachable (so its probably off or not connected, but it has been before), DNS is Domain Name Service. The DNS basically stores the information (your router probably does this). Finally, else it just says it couldn't be resolved which means it wasnt reachable nor was it found looking in the DNS.

I found this code here and here

UPDATE

So if you run this and you just keep getting something like "192.168.0.5/192.168.0.5 the host address and host name are equal, meaning the host name could not be resolved" That means that your router (your local DNS) just isn't storing the information OR those devices just choose not to submit their host name to the router, and that is why you will continually get that message. As far as I am aware, there isn't a way around this because those device names literally aren't stored

0
Ragib On

Try this :)

import java.io.IOException;
import java.net.*;
import java.util.Vector;

public class search {
    public static void main(String args[]) throws UnknownHostException{

        Vector<String> Available_Devices=new Vector<>();
        String myip=InetAddress.getLocalHost().getHostAddress();
        String mynetworkips=new String();

        for(int i=myip.length();i>0;--i) {
            if(myip.charAt(i-1)=='.'){ mynetworkips=myip.substring(0,i); break; }
        }

        System.out.println("My Device IP: " + myip+"\n");

        System.out.println("Search log:");
        for(int i=1;i<=254;++i){
            try {
                InetAddress addr=InetAddress.getByName(mynetworkips + new Integer(i).toString());
                if (addr.isReachable(1000)){
                    System.out.println("Available: " + addr.getHostAddress());
                    Available_Devices.add(addr.getHostAddress());
                }
                else System.out.println("Not available: "+ addr.getHostAddress());

            }catch (IOException ioex){}
        }

        System.out.println("\nAll Connected devices(" + Available_Devices.size() +"):");
        for(int i=0;i<Available_Devices.size();++i) System.out.println(Available_Devices.get(i));
    }
}