我正在尝试创建一个列出本地网络上所有连接设备的功能.
我所做的是ping地址空间x.x.x.0到x.x.x.255的任何地址,但它似乎无法正常工作.有人能以某种方式解释或扩展我的代码吗?我从电话(10.0.0.17)和默认网关(10.0.0.138)得到回复.后者甚至不应该在那里(事实上我不知道默认网关是什么,但忽略了).我虽然错过了这台电脑的IP.
public ArrayList getConnectedDevices(String YourPhoneIPAddress) {
ArrayList ret = new ArrayList();
LoopCurrentIP = 0;
// String IPAddress = "";
String[] myIPArray = YourPhoneIPAddress.split("\\.");
InetAddress currentPingAddr;
for (int i = 0; i <= 255; i++) {
try {
// build the next IP address
currentPingAddr = InetAddress.getByName(myIPArray[0] + "." +
myIPArray[1] + "." +
myIPArray[2] + "." +
Integer.toString(LoopCurrentIP));
// 50ms Timeout for the "ping"
if (currentPingAddr.isReachable(50)) {
if(currentPingAddr.getHostAddress() != YourPhoneIPAddress){
ret.add(currentPingAddr);
}
}
} catch (UnknownHostException ex) {
} catch (IOException ex) {
}
LoopCurrentIP++;
}
return ret;
}