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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值