Salve a tutti ho questo programma un semplice port scanner volevo chiedere se possibbile chiarimenti su alcune righe di codice Grazie.
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) {
String ip = "localhost";
System.out.println("===Scanning " + ip + "===");
try {
ExecutorService es = Executors.newFixedThreadPool(100);
int timeout = 200;
List<Future<ScannerResult>> list = new ArrayList<>();
for (int port = 1; port <= 65535; port++) {
list.add(portOpened(es, ip, port, timeout));
}
es.shutdown();
int openPorts = 0;
for (final Future<ScannerResult> f : list) {
if (f.get().isOpen()) {
openPorts++;
System.out.println(f.get().getPort());
}
}
System.out.println("L'indirizzo " + ip + " ha " + openPorts + " porte aperte");
} catch (InterruptedException | ExecutionException ex) {
System.out.println(ex.getMessage());
}
System.out.println("===END===");
}
public static Future<ScannerResult> portOpened(ExecutorService es, String ip, int port, int timeout) {
return es.submit(() -> {
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), timeout);
socket.close();
return new ScannerResult(port, true);
} catch (Exception ex) {
return new ScannerResult(port, false);
}
});
}
}
Ultima domanda come mai è necessario un timeout se ho gia un ciclo per gestire il programma