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

 

    

你可能感兴趣的:(java,多线程,socket,swing,F#)