dragonfire256
dragonfire256

Reputation: 31

Find ip address of a device

I can't find the ip address of a device connected to my network. I tried using network interface, but only gives me the loopback address and my pc address; the code that i used is:

try
            {
            Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces();
                for (; n.hasMoreElements();)
                {
                        NetworkInterface e = n.nextElement();
                        System.out.println("Interface: " + e.getName());
                        Enumeration<InetAddress> a = e.getInetAddresses();
                        for (; a.hasMoreElements();)
                        {
                                InetAddress addr = a.nextElement();
                                System.out.println("  " + addr.getHostAddress());
                        }
                }
            }catch (Exception e)
              {
               System.out.println(e.toString());
              }

Also, i used PrintServiceLookup,but the methods of that class doesn't give the ip address (the device is a card printer); the code that i used is:

            PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);

            System.out.println("Printer Services found:");
            printService(services);

            PrintService service = PrintServiceLookup.lookupDefaultPrintService();

            if (service!=null) {
                System.out.println("Default Printer Service found:");
                System.out.println(service);
            }

private static void printService(PrintService[] services) {
            if (services!=null && services.length>0) {
                for (int i = 0; i < services.length; i++) {
                    System.out.println(services[i]);
                }
            }
        }

Anyone has a different point of view or perspective to solve the issue?

Upvotes: 0

Views: 776

Answers (1)

extols
extols

Reputation: 1772

NetworkInterface.getInetAddresses() is used to return the local IP address of the network adaptor, hence the reason you are seeing loopback and your primary IP.

It's not possible to enumerate devices on the network in this way. You may need to look into something like SNMP.

Upvotes: 1

Related Questions