Kirk Patrick Brown
Kirk Patrick Brown

Reputation: 184

How to effectively ping and discover all clients on a network?

The problem I am having is acquiring all reachable clients on a network.The below method returns some clients when called. In most cases other android clients.However for the PC it fails when firewall is on.Is there a more effective way to get all clients in Java/android purely or will I need to use android NDK?Any help from experts in this domain will be appreciated.Thanks in advance.

    /***
     *  ping_JavaStyle(final int j)
     *  uses multi threads to enhance performance
     *  while pinging from  0>j<=255
     * @param j
     */
    private void ping_JavaStyle(final int j)
    {
        new Thread(new Runnable() {   // new thread for parallel execution

            public void run() {
                try {
                    String testIp = prefix + String.valueOf(j);
                    InetAddress address = InetAddress.getByName(testIp);
                    String output = address.toString().substring(1);


                    if (address.isReachable(3000)) {
                        System.out.println(output + " is on the network");
                        ipList.add(testIp);
                    } else {

                        if (retest(testIp, 139)) {
                            ipList.add(testIp);
                        } else {
                            System.out.println("Not Reachable: " + output);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }

Upvotes: 0

Views: 127

Answers (1)

Kirk Patrick Brown
Kirk Patrick Brown

Reputation: 184

After Researching some more, got this working.With help of this repo:https://github.com/stealthcopter/AndroidNetworkTools

Below code solves the problem:

  ** RunnableTask.Java
 * Created by Kirk on 10/29/2017.
 */

public class RunnableTask implements Callable<Boolean> {

private String testIp = "";
private Boolean is_Reachable = false;


public RunnableTask(String testIp) {
    this.testIp = testIp;
}

@Override
public Boolean call() throws Exception {
    try {
        PingResult pingResult = Ping.onAddress(this.testIp).setTimes(1).setTimeOutMillis(1500).doPing();

        if (pingResult.isReachable) {
            is_Reachable = true;
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

    return is_Reachable;

  }
}

And use in the caller method:

private static final int NTHREDS = 255;


    //.......
        ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);
        List<Future<Boolean>> thread_Values_list = new ArrayList<>();
        for (int i = 1; i <= 255; i++) {
            final int j = i;
            try {
                try {
                    String testIp = prefix + String.valueOf(j);
                    RunnableTask worker = new RunnableTask(testIp);
                    Future<Boolean> submit = executor.submit(worker);
                    thread_Values_list.add(submit);

                } catch (Exception e) {
                    e.printStackTrace();
                }


            } catch (Exception e) {


            }
        }
        for (Future<Boolean> finishedThread : thread_Values_list) {

            String reachable_Ip = "";
            try {
                if (finishedThread.get()) {
                    reachable_Ip = prefix + String.valueOf(finishThread_counter);
                    ipList.add(reachable_Ip);
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            finishThread_counter++;
        }

        executor.shutdown();
    }

Upvotes: 1

Related Questions