java - 查找设备的IP地址

标签 java ip-address device printers

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

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 的内容.

关于java - 查找设备的IP地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29703053/

相关文章:

java - 复制的文件已损坏

java - Spring 数据按顺序获取映射对象

java - 我将我的线程设置为比其他线程更高的优先级,但 CPU 时间似乎是随机的?

java - 无法使用 GSON 库反序列化对象数据列表

ios - 连接到移动数据swift 3时如何获取IP地址

ios - 当应用程序在设备中处于后台模式时,音频无法播放

iphone - 在设备上调试时,XCode/GDB 在自动继续断点处停止

linux - 在日志中用主机名替换 IP

http - HTTP_CLIENT_IP 和 HTTP_X_FORWARDED_FOR 有什么区别?

c++ - 我在注册表中的哪里可以找到音频设备名称的不同部分