【问题标题】:Find ip address of a device查找设备的ip地址
【发布时间】:2015-04-17 15:11:20
【问题描述】:

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

谁有不同的观点或观点来解决这个问题?

【问题讨论】:

  • 你写的代码不是用来识别同一网络上的设备的!你需要另一种方法!
  • 解释一下@shekharsuman,如果你不介意的话
  • @dragonfire256-原因在extols'回答中提到。

标签: java ip-address device printers


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-09-01
    • 2018-05-19
    • 2018-01-05
    • 1970-01-01
    • 2020-11-01
    • 2014-05-25
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多