【发布时间】: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