【发布时间】:2017-01-29 09:51:42
【问题描述】:
我可以使用 inetaddress 类获取局域网中的所有设备 IP 地址。我需要做的是反向查找 ip 地址并在网络中查找设备名称,例如:“Jimmie's Macbook”
我的代码块可以获取本地网络范围内的所有 IP 地址:
private ArrayList<String> scanSubNet(String subnet) {
ArrayList<String> hosts = new ArrayList<>();
InetAddress inetAddress;
for (int i = 1; i <= 255; i++) {
try {
inetAddress = InetAddress.getByName(subnet + String.valueOf(i));
if (inetAddress.isReachable(1000)) {
hosts.add(inetAddress.getHostName());
Log.d(TAG, InetAddress.getByAddress(inetAddress.getAddress()).getHostAddress());
Log.d(TAG, InetAddress.getByAddress(inetAddress.getAddress()).getCanonicalHostName());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return hosts;
}
我将我的方法称为;
ArrayList<String> subnetList = scanSubNet("192.168.1.");ArrayList<String> subnetList = scanSubNet("192.168.1.");
在 Log.d 中(TAG,我正在尝试使用反向 dns 获取设备名称。但是这两行都给我输出为 ip-address(不是设备名称作为字符串)
有没有成功的机会?
问候, 上。
【问题讨论】:
标签: android dns android-networking inetaddress