我将我的端口扫描器类复制到我的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