!747 m { static int threads = 10; static int timeout = 500; // SHOULD be enough... static String ip = "127.0.0.1"; static class Program { int port; String helloString; *(int *port, String *helloString) {} } p { final ExecutorService es = Executors.newFixedThreadPool(threads); print("Program-scanning " + ip + " with timeout " + timeout + " ms in " + threads + " threads."); startTiming(); new List> futures; for (int port = 1; port <= 65535; port++) { futures.add(checkPort(es, ip, port, timeout)); } es.shutdown(); new List programs; for (final Future f : futures) { Program p = f.get(); if (p != null) programs.add(p); } stopTiming(); System.out.println("Found " + programs.size() + " program(s) on " + ip + ": " + structure(programs)); } public static Future checkPort(final ExecutorService es, final String ip, final int port, final int timeout) { return es.submit(new Callable() { @Override public Program call() { try { Socket socket = new Socket(); socket.setSoTimeout(timeout); socket.connect(new InetSocketAddress(ip, port), timeout); print("Connected to " + ip + ":" + port); BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream(), "UTF-8")); S hello = in.readLine(); socket.close(); return new Program(port, hello); } catch (Exception ex) { return null; } } }); } }