<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    paulwong

    端口掃描程序

    If you need 200ms for each of the 65536 ports (in the worst case, a firewall is blocking everything, thus making you hit your timeout for every single port), the maths is pretty simple: you need 13k seconds, or about 3 hours and a half.

    You have 2 (non-exclusive) options to make it faster:
    • reduce your timeout
    • paralellize your code
    Since the operation is I/O bound (in contrast to CPU bound -- that is, you spend time waiting for I/O, and not for some huge calculation to complete), you can use many, many threads. Try starting with 20. They would divide the 3 hours and a half among them, so the maximum expected time is about 10 minutes. Just remember that this will put pressure on the other side, ie, the scanned host will see huge network activity with "unreasonable" or "strange" patterns, making the scan extremely easy to detect.

    The easiest way (ie, with minimal changes) is to use the ExecutorService and Future APIs: @import url(http://m.tkk7.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

    public static Future<Boolean> portIsOpen(final ExecutorService es, final String ip, final int port, final int timeout) {
      return es.submit(new Callable<Boolean>() {
          @Override public Boolean call() {
            try {
              Socket socket = new Socket();
              socket.connect(new InetSocketAddress(ip, port), timeout);
              socket.close();
              return true;
            } catch (Exception ex) {
              return false;
            }
          }
       });
    }


    Then, you can do something like:

    public static void main(final String args) {
      final ExecutorService es = Executors.newFixedThreadPool(20);
      final String ip = "127.0.0.1";
      final int timeout = 200;
      final List<Future<Boolean>> futures = new ArrayList<>();
      for (int port = 1; port <= 65535; port++) {
        futures.add(portIsOpen(es, ip, port, timeout));
      }
      es.shutdown();
      int openPorts = 0;
      for (final Future<Boolean> f : futures) {
        if (f.get()) {
          openPorts++;
        }
      }
      System.out.println("There are " + openPorts + " open ports on host " + ip + " (probed with a timeout of " + timeout + "ms)");
    }


    If you need to know which ports are open (and not just how many, as in the above example), you'd need to change the return type of the function to Future<SomethingElse>, where SomethingElse would hold the port and the result of the scan, something like:

    public final class ScanResult {
      private final int port;
      private final boolean isOpen;
      // constructor
      
    // getters
    }


    Then, change Boolean to ScanResult in the first snippet, and return new ScanResult(port, true) or new ScanResult(port, false) instead of just true or false





    posted on 2013-03-14 10:10 paulwong 閱讀(382) 評(píng)論(0)  編輯  收藏 所屬分類: 性能優(yōu)化

    主站蜘蛛池模板: 国产精品亚洲精品爽爽| 久久久久久久久久免免费精品| 亚洲AV无码乱码在线观看富二代 | 午夜dj免费在线观看| 亚洲人成网站色在线入口| 国产日本一线在线观看免费| 亚洲?V乱码久久精品蜜桃 | 亚洲免费人成视频观看| 五月婷婷亚洲综合| 青娱乐在线免费观看视频| 日韩中文字幕精品免费一区| 亚洲最大在线观看| 免费av一区二区三区| 四虎永久免费地址在线网站| 亚洲国产日韩在线人成下载| 成全视成人免费观看在线看| 全免费A级毛片免费看网站| 亚洲AV综合色区无码一二三区| 最近免费视频中文字幕大全| 狠狠亚洲狠狠欧洲2019| 国产偷国产偷亚洲高清在线| 免费又黄又爽又猛的毛片| 亚洲国产视频久久| 中文免费观看视频网站| 亚洲人成色99999在线观看| 69成人免费视频| 亚洲精品综合久久中文字幕| 日本黄色动图免费在线观看| 亚洲真人无码永久在线| 亚洲AV色无码乱码在线观看| 最近免费中文字幕视频高清在线看 | 亚洲日产乱码一二三区别| 亚洲国产成人乱码精品女人久久久不卡 | 日本不卡在线观看免费v| 中文字幕亚洲综合小综合在线| 91免费国产精品| 亚洲国产精品精华液| 最新亚洲成av人免费看| 国内精品免费麻豆网站91麻豆| 日本系列1页亚洲系列| 又粗又大又猛又爽免费视频|