我將我的端口掃描器類複製到我的android項目中,而不更改源代碼中的任何內容。 Eclipse沒有抱怨代碼,但我的代碼似乎並沒有在android中正常工作。這是我的整個portscanner代碼。Android項目中的J2ME類
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class PortScanner {
private final String ip;
private final int sPort, ePort, timeout, poolSize;
private ArrayList<Integer> openPorts = new ArrayList();
private final ExecutorService es;
private Collection<Future<?>> futures = new LinkedList<>();
public PortScanner(String ip, int sPort, int ePort, int timeout, int poolSize) {
this.ip = ip;
this.sPort = sPort;
this.ePort = ePort;
this.timeout = timeout;
this.poolSize = poolSize;
es = Executors.newFixedThreadPool(this.poolSize);
}
public ArrayList<Integer> getPorts() {
return openPorts;
}
public void runScanner() {
for (int startPort = sPort; startPort <= ePort; startPort++) {
futures.add(es.submit(new Check(ip, startPort, timeout)));
}
es.shutdown();
}
private class Check implements Runnable { // BEGININ OF INNER CLASS
private String ip;
private int port, timeout;
private Check(String ip, int port, int timeout) {
this.ip = ip;
this.port = port;
this.timeout = timeout;
}
@Override
public void run() {
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), timeout);
socket.close();
openPorts.add(port);
} catch (Exception ex) {
}
}
} // END OF INNER CLASS
}
我測試我的代碼在幾個不同的方式,它確保只有正確不工作就是run方法內部類,因爲當我手動添加一些端口,它們能打印數組列表,但是當我LAT我的掃描儀添加它們返回總是空的。相同的端口掃描代碼在桌面上完美工作。有誰知道我做錯了什麼?
安置自己的logcat的。 – 2012-07-29 09:56:28
你的意思是1行文字,其中是ID,Appliacation number,Tag等? – 2012-07-29 09:59:23
http://www.appaholics.in/using-the-logcat/ – 2012-07-29 10:00:52