package com.safety;


import cn.hutool.http.HttpUtil;

import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Java实现多线程扫描开放端口
 * @author Administrator
 *
 */
public class Demo extends Thread {
    public static void main(String[] args) throws InterruptedException {
        AtomicInteger count=new AtomicInteger(1);
        CopyOnWriteArrayList<Integer> objects = new CopyOnWriteArrayList<>();
        CountDownLatch countDownLatch = new CountDownLatch(65536);
        ExecutorService executorService = Executors.newFixedThreadPool(2500);
        for (int i = 1; i <= 65536; i++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try{
                        int i1 = count.addAndGet(1);
                        System.err.println("准备扫描:"+i1);
                        String body = HttpUtil.createGet("81.69.57.61:"+i1).execute().body();
                        System.err.println("====================================端口:"+i1);
                        objects.add(i1);
                        countDownLatch.countDown();
                    } catch (Exception e) {
                        countDownLatch.countDown();
                    }
                }
            });
            Thread.sleep(1);
        }
        countDownLatch.await();
        System.err.println("扫描到以下端口:");
        objects.forEach(item->{
            System.err.print(item+"-");
        });
        System.err.println("=====================");
    }
}

 

 需要hutool