日期:2014-05-20  浏览次数:20801 次

如何快速扫描局域网内所有IP的某一个端口是否开启
例如:

本机IP: 123.333.222.10

需要扫描 IP: 123.333.222.1:6610 - 123.333.222.224:6610 判断端口是否开启 或者说是否有server在监听这个端口

请问如何快速(1秒内)完成上面的224个执行(from 1 to 224)

我自己贴一段代码 可以用是可以用 但是太慢了 
本来想用
Java code

ExecutorService executor = Executors.newSingleThreadExecutor();
executor.invokeAll(Arrays.asList(new Task(ipHead, tail, 6610)),
200, TimeUnit.MILLISECONDS);
executor.shutdown();

来提到效率的 但是好像没用 不知道是什么问题 


runnable code:

Java code

import java.net.*;
import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.io.*;

public class JPortScanner1 {

    public JPortScanner1(String IP, int port) {

    }

    public static void main(String[] args) throws IOException,
            InterruptedException {

        String ipHead;
        int lastPoint;
        String IP = InetAddress.getLocalHost().getHostAddress();

        lastPoint = IP.lastIndexOf('.');

        ipHead = IP.substring(0, ++lastPoint);

        for (int tail = 234; tail < 255; tail++) {
            ExecutorService executor = Executors.newSingleThreadExecutor();
            executor.invokeAll(Arrays.asList(new Task(ipHead, tail, 6610)),
                    200, TimeUnit.MILLISECONDS);
            executor.shutdown();
        }

    }
}

class Task implements Callable<String> {
    String ipHead;
    int ipTail;
    int port;

    public Task(String ipHead, int ipTail, int port) {
        this.ipHead = ipHead;
        this.ipTail = ipTail;
        this.port = port;
    }

    public String call() throws Exception {
        Socket connect = new Socket();
        connect.setSoTimeout(100);
            try {
            
                connect = new Socket(this.ipHead + ipTail, this.port);
                connect.close();

                System.out.println("Open port:" + this.ipHead + ipTail + " "
                        + this.port);

            } catch (UnknownHostException e) {
                System.out.println("Unknown Port:" + this.ipHead + ipTail + " "
                        + this.port);
            } catch (IOException e) {
                System.out.println("Unknown Port:" + this.ipHead + ipTail + " "
                        + this.port);
            } 
        
        System.out.println("Finished!");
        return null;

    }
}





------解决方案--------------------
不动手无说服力(并不说我是个高手,只是想说怎么这么多人不去思考动手呢)
我测试的一个示例也就200多微妙(双核CPU):
Java code

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class JPortScanner1 {

    public static void main(String[] args) throws InterruptedException, UnknownHostException {
        String ipHead;
        int lastPoint;
        String IP = InetAddress.getLocalHost().getHostAddress();

        lastPoint = IP.lastIndexOf('.');

        ipHead = IP.substring(0, ++lastPoint);

        ExecutorService executor = Executors.newCachedThreadPool();
        Collection<Callable<String>> list = new LinkedList<Callable<String>>();
        long start = System.currentTimeMillis();
        for (int tail = 1; tail < 255; tail++) {
            list.add(new Task(ipHead, tail, 8081));
        }
        executor.invokeAll(list);
        executor.shutdown();
        long end = System.currentTimeMillis();
        System.out.println("All time: " + (end - start) + " milliseconds");
    }

}

class Task implements Callable<String> {
    String ipHead;
    int ipTail;
    int port;

    public Task(String ipHead, int ipTail, int port) {
        this.ipHead = ipHead;
        this.ipTail = ipTail;
        this.port = port;
        System.out.println(ipHead + ipTail + ":" + port);
    }

    public String call(){
        Socket connect = new Socket();
        try {
            connect.connect(new InetSocketAddress(ipHead + ipTail, port), 100);
             while(true){
                    if(connect.isConnected()){
                        //System.out.println(ipHead + ipTail + ":" + port + " success");
                        break;
                    }
                }
                connect.close();
        } catch (IOException e) {
            //System.out.println(ipHead + ipTail + ":" + port + " failure");
        }
        return null;
    }
}