繁体   English   中英

查找设备的IP地址

[英]Find ip address of a device

我找不到连接到网络的设备的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]);
                }
            }
        }

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

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

这样无法枚举网络上的设备。 您可能需要研究类似SNMP的内容

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM