码迷,mamicode.com
首页 > 编程语言 > 详细

线程分段扫描端口

时间:2014-11-21 14:23:19      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   java   for   

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

 

线程分段扫描端口

标签:style   blog   io   ar   color   os   sp   java   for   

原文地址:http://www.cnblogs.com/starzou/p/4112701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!