繁体   English   中英

Java多线程端口扫描程序

[英]Java Multithreaded Port Scanner

试图使其比现在更快。 它的速度非常慢,线程似乎无法同时运行,无法弄清楚。 如果有人可以帮助描述我的问题所在,以便我能弄清楚如何使其更快地运行,我将不胜感激,非常感谢!

package infoGrabber;

import java.awt.List;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class infoMain {

    public static int port;

    @SuppressWarnings("resource")
    public static void main(String[] args) {
        System.out.println("What host do you want to lookup?: ");
        Scanner userEntry = new Scanner(System.in);
        String host = userEntry.nextLine();

        try {
            startThreads(host);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    private static void startThreads(String host) throws InterruptedException {
        int numThreads = 10;
        int count = 10;
        Thread[] threads = new Thread[numThreads];
        System.out.println("Creating threads");
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(new Runner(host, count));
            threads[i].start();
            threads[i].join();
        }
        System.out.println("Done");
    }
}

class Runner implements Runnable {
    static int port;
    private final String host;
    private final int count;
    infoMain main = new infoMain();

    public Runner(String host, int count) {
        this.host = host;
        this.count = count;
    }

    public void run() {
        for (int port = 0; port < 2000; port++) {
            // System.out.println(name + "=" + i + "\n");
            Socket socket;
                try {
                    socket = new Socket(host, port);// Attempt to establish a socket on port i.
                    // If no IOException thrown, there must
                    // be a service running on the port.
                    System.out.println("Port " + port + " is open.");
                    socket.close();
                } catch (IOException ioEx) {
                    System.out.println("Port " + port + " is not open.");
                }// No server on this port
            }
            Thread.yield();
        }
    }

您不会在线程之间划分工作。 相反,您要给每个线程相同的工作量,就像在顺序程序中给主线程一样。 您应该在线程之间划分工作,以提高执行速度。

您的循环应类似于:

 for (int i = 0; i < threads.length; i++) {
     threads[i] = new Thread(new Runner(host, count * NUMBER_OF_PORTS_PER_THREAD));
     threads[i].start();

 }

 // Join threads after you created them
 // All credits for @biziclop for spotting this
 for (int i = 0; i < threads.length; i++) {
     threads[i].join();
 }

您的Runnable代码应类似于:

class Runner implements Runnable {

final private int startPort;

public Runner(String host, int startPort) {
        this.host = host;
        this.startPort = startPort;
    }

    public void run() {
        for (int port = startPort; port <= NUMBER_OF_PORTS_PER_THREAD + startPort; port++) {
        ...
    }
}

其中NUMBER_OF_PORTS_PER_THREAD应该等于200,以扫描具有10个线程的2000个端口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM