JAvaマルチスレッド高速スキャンマシンポート...


会社に入ってしばらく时间があって、基本的に中で忙しくない时いくつかのswingツールを书いて、先周の时ホストはポートを交换して、私达のこちらは情报を得ていないで、TILは比较的に紧急です...東東を書いてscanに行って...わかった.=.=...pingが良ければ60000個のポート約2-3分程度..
    public static void main(String args[]) throws InterruptedException {


        ExecutorService pool = Executors.newFixedThreadPool(500);
        long s = System.currentTimeMillis();
        final Vector list = new Vector();
        final Vector all = new Vector();
        int startport = 1 << 10;
        int endport = 1 << 16;

        for (int i = 1024; i < endport; i++) {
            final int port = i;
            all.add(new Callable() {
                boolean f=true;
                public Object call() throws Exception {
                   try {
                        Socket socket = new Socket("localhost", port);
                        list.add(port + "");
                    } catch (UnknownHostException e) {
                        // TODO Auto-generated catch block
                        f=false;

                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        f=false;
                    }finally{
                        if(f) System.out.println("scan port==>"+port);
                        return port;
                    }
                }
            });

        }

        System.out.println("scanning...");
        try {
            pool.invokeAll(all);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + ",");
        }
        System.out.println("scan over...");
        long e = System.currentTimeMillis();
        System.out.println("exec time==>" + (e - s) + "ms");
    }

印刷情報:
 
 
  
と書く
scanning...
scan port==>1071
scan port==>1279
scan port==>1127
scan port==>1054
scan port==>2021
scan port==>2738
scan port==>5005
scan port==>5009
scan port==>5001
scan port==>5080
scan port==>5900
scan port==>6789
scan port==>7474
scan port==>7500
scan port==>7580
scan port==>8001
scan port==>18081
scan port==>20161
scan port==>60001
1071,1279,1127,1054,2021,2738,5005,5009,5001,5080,5900,6789,7474,7500,7580,8001,18081,20161,60001,scan over...
exec time==>130534ms