我将端口扫描器类复制到了我的 android 项目中,而没有更改源代码中的任何内容。Eclipse 没有抱怨代码,但我的代码似乎在 android 中无法正常工作。这是我的整个端口扫描仪代码。
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
}
我以几种不同的方式测试了我的代码,并且它确定唯一不能正常工作的是内部类运行方法,因为当我手动将一些端口添加到数组列表时它们会打印,但是当我 lat 我的扫描仪添加它们时它总是返回空的。相同的端口扫描代码在桌面上完美运行。有谁知道我做错了什么?