我已经写了这个代码在端口6789上ping类c的IP地址,当我点击一个名为PING的按钮时,线程开始,它将检索所有具有端口6789打开的ip地址...但我需要的是刷新(重新ping)每个,让说5秒,并添加ips最近加入,如果存在,并省略离开端口。
我调试了几个小时,但没有希望!!!
在此先感谢:DD
Thread pingo = new Thread(new Runnable() {
public void run() {
if (readableNetmask.equals("255.255.255.0")) {
for (int i = 2; i <= 254; i++) {
String ip_address = readableIPAddress;
String oct1 = "", oct2 = "", oct3 = "", oct4 = "";
StringTokenizer stok = new StringTokenizer(
ip_address, ".");
while (stok.hasMoreTokens()) {
oct1 = stok.nextToken();
oct2 = stok.nextToken();
oct3 = stok.nextToken();
oct4 = stok.nextToken();
}
to_ping_ip = oct1 + "." + oct2 + "." + oct3 + "."
+ String.valueOf(i);
if (pingAddress(to_ping_ip, 6789)) {
handler.post(new UpdateIPListViewRunnable());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// ips_List.add(to_ping_ip);
}
}
}
}
});
pingo.start();PingAddress()函数:
public boolean pingAddress(String ip, int port) {
Socket socket = new Socket();
try {
socket.connect(new InetSocketAddress(ip, port), 200);
socket.close();
} catch (IOException e) {
return false;
}
return true;
}列出出现地址的地方:
static public class UpdateIPListViewRunnable implements Runnable {
public void run() {
arrayAdapter.add(to_ping_ip);
arrayAdapter.notifyDataSetChanged();
}
}