Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

53
LINES

< > BotCompany Repo | #1000802 // Program scan!

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (253L/3K/10K).

!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<Future<Program>> futures;
    for (int port = 1; port <= 65535; port++) {
      futures.add(checkPort(es, ip, port, timeout));
    }
    es.shutdown();
    new List<Program> programs;
    for (final Future<Program> 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<Program> checkPort(final ExecutorService es, final String ip, final int port, final int timeout) {
    return es.submit(new Callable<Program>() {
        @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;
          }
        }
     });
  }
}

Author comment

Began life as a copy of #1000796

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000802
Snippet name: Program scan!
Eternal ID of this version: #1000802/1
Text MD5: d3749be90a34534028fa6d8cb6723e2d
Transpilation MD5: aaac5f66b439030461c99c243c6bbd99
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-28 16:34:13
Source code size: 1706 bytes / 53 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 594 / 628
Referenced in: [show references]