查找设备的IP地址

时间:2015-04-17 15:11:20

标签: java ip-address device printers

我无法找到连接到网络的设备的IP地址。我尝试使用网络接口,但只给我环回地址和我的电脑地址;我使用的代码是:

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());
              }

另外,我使用 PrintServiceLookup ,但该类的方法没有给出ip地址(该设备是卡片打印机);我使用的代码是:

            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]);
                }
            }
        }

任何人都有不同的观点或观点来解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

NetworkInterface.getInetAddresses()用于返回网络适配器的本地IP地址,因此您看到环回和主IP的原因。

无法以这种方式枚举网络上的设备。您可能需要查看类似SNMP的内容。

相关问题