How To Scan Open TCP Ports of Multiple IPs Simultaneously Applying Java

This is a simple multi-threaded Java application which scan tcp ports of multiple machines Simultaneously. I applied Observer design pattern to notify user if a port on those machines is accessible or not.  Like the use of interfaces, the Observer design pattern can be used to decouple components and enable extensibility without modification. It also contributes to achieving separation of concerns.
The following is the port scanner class. It should be observable to the observer (ObserverNotifier) Coinstar locations class. So it extends Observable class:

package com.hexican.portscanner;

import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Observable;
import java.io.IOException;

public class PortScanner extends Observable implements Runnable {
    private String target;
    private int fromPort = 0;   //default start port number
    private int toPort = 65535 ; //default end port number

    public void setFromPort(int fromPort) { // setter
        this.fromPort = fromPort;
    }

    public void setToPort(int toPort) { // setter
        this.toPort = toPort;
    }

    public void setTarget(String target) { // setter
        this.target = target;
    }

    public void run() {
        InetAddress inetAddress = null;
        try {
            inetAddress = InetAddress.getByName(target);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        String hostName = inetAddress.getHostName();
        for (int port = fromPort; port <= toPort; port++) {
            try {
                Socket socket = new Socket(hostName, port);
                socket.close();
                setChanged();
                notifyObservers(port+ " of "+ hostName);
            } catch (IOException e) {
                // do nothing.
            }
        }
    }
}

Also the following is the ObserverNotifier which is waiting to be notified by port scanner class at the time an open port identified.

package com.hexican.portscanner;

import java.util.Observable;
import java.util.Observer;

public class ObserverNotifier implements Observer {
    private String resp;
    public void update (Observable obj, Object arg) {
        if (arg instanceof String) {
            resp = (String) arg;
            System.out.println("\n Port No "+ resp +" is accessible.");
        }
    }
}

We need a class to start observable and observer both. The following class scans two separated host (hexican.com and yahoo.com).  Multithreading and separation of port scanning and the notifier keeps the code simple and expandable.

package com.hexican.portscanner;

public class MainApp {
    public static void main(String args[]) {
        System.out.println("Scanning...");

        final ObserverNotifier observer1 = new ObserverNotifier();
        final PortScanner observable1 = init("yahoo.com", 80 , 90 , observer1);

        final ObserverNotifier observer2 = new ObserverNotifier();
        final PortScanner observable2 = init("hexican.com", 0 , 200 , observer2);

        Thread thread1, thread2;

        thread1 = new Thread(observable1);
        thread1.start();

        thread2 = new Thread(observable2);
        thread2.start();
    }

    private static PortScanner init(String ipAddress, int fromPort, int toPort, ObserverNotifier observer ){
        PortScanner observable = new PortScanner();
        observable.setTarget(ipAddress);
        observable.setFromPort(fromPort);
        observable.setToPort(toPort);
        observable.addObserver( observer );
        return observable;
    }
}

Done.

This entry was posted in Java, Networking, Software Engineering. Bookmark the permalink.

3 Responses to How To Scan Open TCP Ports of Multiple IPs Simultaneously Applying Java

  1. daigoumee says:

    Beneficial info and excellent design you got here! I want to thank you for sharing your ideas and putting the time into the stuff you publish! Great work!

  2. Rustem Gunes says:

    I ‘ve run this code, but got this exception.

    java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.(Unknown Source)
    at java.net.Socket.(Unknown Source)
    at com.security.portscanner.PortScanner.run(PortScanner.java:44)
    at java.lang.Thread.run(Unknown Source)

Leave a Reply