2017-10-29 174 views
0

我遇到的問題是獲取網絡上所有可到達的客戶端。下面的方法在調用時會返回一些客戶端。在大多數情況下,其他Android客戶端。然而,對於PC它在防火牆打開時失敗。是否有一種更有效的方式來獲得Java/Android中的所有客戶端純粹或將需要使用android NDK?此域中的專家的任何幫助將謝謝。提前感謝。如何有效地ping和發現網絡上的所有客戶端?

/*** 
    * 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(); 


    } 
+1

檢查https://github.com/rorist/android-network-discovery/ –

回答

1

研究一些後,得到了這個回購該working.With幫助:https://github.com/stealthcopter/AndroidNetworkTools

下面的代碼解決了這個問題:

** 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; 

    } 
} 

而且在來電者使用方法:

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