我找不到连接到我的网络的设备的 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/