侧边栏壁纸
博主头像
seems 博主等级

学习博客

  • 累计撰写 62 篇文章
  • 累计创建 41 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

JAVA功能代码-端口扫描

seems
2023-09-01 / 0 评论 / 0 点赞 / 9 阅读 / 0 字
  • domain为IP地址,从startport端口开始,扫描到endport端口为止
public void test(String domain,int startport,int endport) {
    if (startport > endport){
        System.out.println("输入参数错误!");
        return;
    }
    int flag=0;
    LinkedList<Thread> threadPool = new LinkedList<Thread>();
    for (int i = startport ; i<endport ; i++ ){
        int port = i;
        Socket socket = new Socket();
        Runnable run = new Runnable() {
            @Override
            public void run() {
                try {
                    socket.connect(new InetSocketAddress(domain,port));
                    System.out.println("端口"+port+":开放");
                } catch (IOException e) {
                    // System.out.println("端口"+port+":关闭");
                }
            }
        };
        flag++;
        Thread th = new Thread(run);
        th.start();
        threadPool.add(th);
    }
    Runnable runobserv = new Runnable() {
        @Override
        public void run() {
            boolean alljobisover = false;
            while (!alljobisover){
                alljobisover = true;
                for (Thread thread : threadPool){
                    if (thread.isAlive()){
                        alljobisover = false;
                        break;
                    }
                }
            }
        }
    };
    Thread t1 = new Thread(runobserv);
    t1.start();
    System.out.println("flag:"+flag);
}
0

评论区