Monday, July 14, 2014

MultiThreaded Program to Check and Display the Reachable IP addresses on a Network [Java]

This program uses multiple threads to look out for reachable IPs on a network and then displays the IPs that are reachable. You can adjust the timeout in inReachable(timeout) [in milliseconds] and the number of threads in the for loop's check in main function.


import java.io.IOException;
import java.util.concurrent.*;
import java.util.regex.*;
import java.util.*;
import java.net.*;

class Worker implements Runnable {

    static int id = 0;
    final int iden = ++id;
    static volatile String ip = "192.168.80.1";
    static Pattern pattern = Pattern.compile(".\\d+$");
    static Matcher m = null;
    static volatile int i = 1;
    static volatile boolean cancel = false;
    static volatile List<String> reachables = new ArrayList<>();

    public static synchronized void ping(int id, String ipLocal) throws UnknownHostException, IOException {
        InetAddress adr = null;
        if (i < 255) {

            m = pattern.matcher(ip);
            if (m.find()) {
                ip = m.replaceFirst("." + Integer.toString(++i));
                System.out.print("\nThread #" + id + " testing IP: " + ip);

                adr = InetAddress.getByName(ip);
                ipLocal = ip;
            }
        } else {
            cancel = true;
            return;
        }

        if (adr.isReachable(2000)) {
            System.out.print("\nAddress " + ipLocal + " is reachable!");
            reachables.add(ipLocal);
        } else {
            System.out.print("\nAddress " + ipLocal + " not reachable!");
        }
    }

    public void run() {
        String ipLocal = "";
        while (!cancel) {
            try {
                ping(iden, ipLocal);
                Thread.yield();
            } catch (IOException ex) {
                System.out.println("IOException caught!");
            }

        }
    }
}

public class Ping {

    public static void main(String[] args) throws InterruptedException {
        ExecutorService exec = Executors.newCachedThreadPool();

        for (int j = 0; j < 10; ++j) {
            exec.execute(new Worker());
        }

        exec.shutdown();
        while (true) {
            if (exec.isTerminated()) {
                System.out.println("\n\nReachable IPs = ");
                for (String reach : Worker.reachables) {
                    System.out.println(reach);
                }
                break;
            }
        }
    }
}

Sample Output:

Thread #1 testing IP: 192.168.80.2
Address 192.168.80.2 is reachable!
Thread #10 testing IP: 192.168.80.3
Address 192.168.80.3 not reachable!
Thread #10 testing IP: 192.168.80.4
Address 192.168.80.4 not reachable!
Thread #10 testing IP: 192.168.80.5
Address 192.168.80.5 not reachable!
Thread #10 testing IP: 192.168.80.6
Address 192.168.80.6 not reachable!
Thread #10 testing IP: 192.168.80.7
Address 192.168.80.7 is reachable!
Thread #10 testing IP: 192.168.80.8
Address 192.168.80.8 not reachable!


Reachable IPs = 
192.169.80.2
192.168.80.7


No comments:

Post a Comment