5
我試圖創建一個列出本地網絡上所有連接的設備的功能。 我所做的是將任何地址從地址空間x.x.x.0 ping到x.x.x.255,但它似乎不能正常工作。任何人都可以解釋或擴展我的代碼?我確實收到了電話(10.0.0.17)和默認網關(10.0.0.138)的響應。後者甚至不應該在那裏(事實上,我不知道默認網關是什麼,但忽略了這一點)。我錯過了這臺電腦的IP。列出本地網絡上的設備與ping
public ArrayList<InetAddress> getConnectedDevices(String YourPhoneIPAddress) {
ArrayList<InetAddress> ret = new ArrayList<InetAddress>();
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;
}
順便說一句,我不使用模擬器,我用我的手機! – rtc11