java测速及端口开放扫描

直接上代码

package com.demo.www.thread;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyThreadPool {
          
   
    //socket系统本来默认20s太久了这里改成1000
    public static int SOCKET_TIMEOUT=1000;
    //创建固定大小的线程池
    public static ExecutorService executorService = Executors.newFixedThreadPool(4);
//    //创建只有一个线程的线程池
//    ExecutorService executorService1 = Executors.newSingleThreadExecutor();
//    //创建无限执行线程池
//    ExecutorService executorService2 = Executors.newCachedThreadPool();

    public static void main(String[] args) {
          
   
        String serverIp="192.168.11.155";
        String ports="80,21,22,3306,3307,6379,6378,7077,8848,9000,9004,8080";
        long stime=System.currentTimeMillis();
        Map<String,Long>portMaps=MyThreadPool.sumbitTask(serverIp,ports);
        long etime=System.currentTimeMillis();

        String [] portsArr=ports.split(",");
        for (String item :portsArr){
          
   
            long speed=portMaps.get(item);
            System.out.println("IP:"+serverIp+" port:"+item+" is "+(speed==-1?"closed":"open"+" speeds:"+speed+"ms"));

        }

        System.out.println("执行完成:"+portMaps.size()+" 耗时:"+(etime-stime)+"ms");
    }







    public static  Map<String,Long> sumbitTask(String ip,String ports){
          
   
        String [] portsArr=ports.split(",");
        Map<String,Long> maps=new HashMap<>();
        for (String item :portsArr){
          
   
            int port= Integer.parseInt(item);
            long speed=-1;
            try {
          
   
                speed=  executorService.submit(()-> MyThreadPool.isOpen(ip,port,MyThreadPool.SOCKET_TIMEOUT)).get();
            } catch (InterruptedException e) {
          
   
                e.printStackTrace();
            } catch (ExecutionException e) {
          
   
                e.printStackTrace();
            }
            maps.put(item,speed);
        }
        return  maps;
    }

    /***
     * 返回延时 返回-1表示关闭
     * @param serverIp
     * @param serverPort
     * @param timeout ms
     * @return
     */
    public static long isOpen(String serverIp,int serverPort,int timeout){
          
   
        long speed=-1;
        Socket client;
        try {
          
   
            long stime=System.currentTimeMillis();
//            client = new Socket(serverIp, serverPort);
            client = new Socket();
            client.connect(new InetSocketAddress(serverIp, serverPort), timeout); //默认的就是20s太久
            long etime=System.currentTimeMillis();
            speed=etime-stime;
            client.close();
        } catch (IOException e) {
          
   
//            e.printStackTrace();
            speed=-1;
        }
        System.out.println("current:"+serverPort+" port:"+serverPort+" speeds:"+speed+"ms");
        return speed;
    }
}
经验分享 程序员 微信小程序 职场和发展