线程分段扫描端口

import java.net.Socket;

public class portScan extends Thread {

    private int minPort; 
    private int maxPort;
    
    public portScan(int minPort, int maxPort) {
        
        this.minPort = minPort;
        this.maxPort = maxPort;
    }
    
    public void run() {
        
        for(int i=minPort; i<=maxPort; i++) {
            try {
                Socket socket = new Socket("127.0.0.1", i);
                System.out.println(String.valueOf(i) + ":ok");
                socket.close();
            }
            catch (Exception e) {
                
            }
        }
    }
    
    
    public static void main(String[] args) {
        
        int minPort = Integer.parseInt(args[0]);
        int maxPort = Integer.parseInt(args[1]);
        int threadCount = Integer.parseInt(args[2]);
        
        int portIncrement = ((maxPort - minPort + 1) / threadCount) + (((maxPort - minPort + 1) % threadCount) == 0 ? 0 : 1);
        portScan[] portscan = new portScan[threadCount];
        
        for(int i=0; i<threadCount; i++) {
            portscan[i] = new portScan(minPort + portIncrement * i, minPort + portIncrement + portIncrement * i -1);
            portscan[i].start();
        }
    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。