dragon40226
dragon40226

Reputation: 257

Java Multithreaded Port Scanner

Trying to get it to go faster than it is now. it's super slow, the threads dont seem to go at the same time, cant figure it out. If anyone could help describe where my problem is so that i can figure out how to make it go faster i would really appreciate it, Thank you very much!

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

Upvotes: 0

Views: 2770

Answers (1)

Ivan Mushketyk
Ivan Mushketyk

Reputation: 8285

You do not divide work between your threads. Instead you give each thread the same amount of work, as you would give to the main thread in a sequential program. You should divide work between your threads in order to increase speed of execution.

Your loop should look something like:

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

And you Runnable code should look something like:

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++) {
        ...
    }
}

where NUMBER_OF_PORTS_PER_THREAD should be equal to 200 to scan 2000 ports with 10 threads.

Upvotes: 1

Related Questions