2016-02-26 69 views
1

我試圖做一個項目,必須使我能夠找到本地設備在同一網絡和信息有關其IP,MAC地址和vendor.Everything是好迄今,但問題是我通過這個代碼,它只發現到手機,並以某種方式沒有看到我的筆記本電腦?我測試了這個代碼,它只顯示我的平板電腦和手機,但沒有電腦。如果你有任何建議,請現在讓我。提前感謝。這裏是我的代碼belove。發現本地網絡設備和IP Programmaticaly

//This class is my pinger class 

public class DiscoverRunner implements Runnable { 
private List<InetAddress> results; 

private String subnet; 
private Integer startAdd; 
private Integer numAdds; 

public DiscoverRunner(String subnet, Integer start, Integer steps) { 
    this.subnet = subnet; 
    this.startAdd = start; 
    this.numAdds = steps; 
    results = new LinkedList<InetAddress>(); 
} 




@Override 
public void run() { 


    int timeout=4000; 
     for (int i=startAdd;i<startAdd+numAdds;i++){ 

      String host=subnet +"." + i; 
      try { 
       InetAddress a = InetAddress.getByName(host); 


      if (a.isReachable(timeout)){ 

       results.add(a); 
       //System.out.println(host + " is reachable"); 
       } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     } 

} 

public List<InetAddress> getResults(){ 
    return results; 
} 

}

這裏是我的另一個類,其中我處理線程執行

public class Pinger { 

private static final int NUMTHREADS = 254; 
public static String myMacAddress = ""; 

public static ArrayList<Device> getDevicesOnNetwork(String subnet) throws IOException { 
    LinkedList<InetAddress> resAddresses = new LinkedList<InetAddress>(); 
    DiscoverRunner[] tasks = new DiscoverRunner[NUMTHREADS]; 

    Thread[] threads = new Thread[NUMTHREADS]; 


    //Create Tasks and treads 
    for (int i = 0; i < NUMTHREADS; i++) { 
     tasks[i] = new DiscoverRunner(subnet,i,1); 
     threads[i] = new Thread(tasks[i]); 
    } 
    //Starts threads 
    for (int i = 0; i < NUMTHREADS; i++) { 
     threads[i].start(); 
    } 

    for (int i = 0; i < NUMTHREADS; i++) { 
     try { 
      threads[i].join(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 

    for (int i = 0; i < NUMTHREADS; i++) { 
     for (InetAddress a : tasks[i].getResults()) { 
      try { 
       a = InetAddress.getByName(a.getHostAddress()); 

      } catch (UnknownHostException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      resAddresses.add(a); 
     } 

    } 

    ArrayList<Device> foundDev = new ArrayList<Device>(resAddresses.size()); 


    for (InetAddress a : resAddresses) { 
     foundDev.add(new Device(a.getHostAddress(), getMacFromArpCache(a.getHostAddress()), a.getCanonicalHostName(), getVendorName(getMacFromArpCache(a.getHostAddress())))); 
    } 


    return foundDev; 
} 

/** 
* Try to extract a hardware MAC address from a given IP address using the 
* ARP cache (/proc/net/arp).<br> 
* <br> 
* We assume that the file has this structure:<br> 
* <br> 
* IP address  HW type  Flags  HW address   Mask  Device 
* 192.168.18.11 0x1   0x2   00:04:20:06:55:1a  *  eth0 
* 192.168.18.36 0x1   0x2   00:22:43:ab:2a:5b  *  eth0 
* 
* @param ip 
* @return the MAC from the ARP cache 
*/ 
public static String getMacFromArpCache(String ip) { 


    if (ip == null) { 
     return null; 
    } 

    BufferedReader br = null; 

    try { 


     br = new BufferedReader(new FileReader("/proc/net/arp")); 

     String line; 
     while ((line = br.readLine()) != null) { 
      String[] splitted = line.split(" +"); 


      if (splitted != null && splitted.length >= 4 && ip.equals(splitted[0])) { 
       // Basic sanity check 
       String mac = splitted[3]; 

       if (mac.matches("..:..:..:..:..:..")) { 
        return mac; 

       } else { 
        return null; 

       } 
      } 


     } 
     return myMacAddress; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      if (br != null) { 
       br.close(); 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return null; 
} 


public static String getVendorName(String MacAddress) throws IOException { 

    try { 
     URL url = new URL("http://api.macvendors.com/" + MacAddress); 

     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setRequestMethod("POST"); 

     InputStream in = new BufferedInputStream(conn.getInputStream()); 
     String vendorName = org.apache.commons.io.IOUtils.toString(in, "UTF-8"); 


     return vendorName; 
    } catch (MalformedURLException e) { 


     return null; 
    } catch (ProtocolException e) { 

     return null; 
    } catch (IOException e) { 

     return null; 
    } 
} 

}

+0

您是否正在訪問網絡無線?那麼它可能是其他計算機從局域網連接,你可以檢查你提供的子網是否封裝局域網子網。 – sgpalit

+0

@sgpalit是的我正在訪問我的家中的接入點(無線適配器),並且我的筆記本電腦也正在訪問wifi沒有以太網連接。 – lifeanddeath

+0

@sgpalit btw,我的筆記本電腦本地IP爲192.168.1.5,你可以從上面看到它從1到255的ping,我不明白爲什麼它不能在這種情況下找到它 – lifeanddeath

回答

0

本平,你在命令行上做,也許你可以改變你的編碼風格。

try { 
     Runtime rt = Runtime.getRuntime(); 
     Process p = rt.exec("ping 192.168.0.142"); 

     BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); 
     String line; 
     while ((line = input.readLine()) != null) { 
      System.out.println(line); 
      p.destroy(); 
      break; 
     } 
     input.close(); 
} catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
} 
+0

兄弟非常感謝您的幫助,但我沒有明確地表達您的意思?因爲我在這裏分享我的whoel代碼和所有這些回收者的意見和類似的東西,所以它是如何工作的?當你運行這個,你是否在你的手機上試過,它也發現你的筆記本電腦?你建議我現在做些什麼? – lifeanddeath

+0

這是不工作的兄弟,有什麼不對勁:)) – sgpalit

+0

嗨兄弟,我只是想出了一些東西..我們剛剛下載同樣的項目給我的朋友電腦,當我們運行它,它工作絕對好,並檢測到的一切,但當我們做同樣的ın我的網絡筆記本電腦只是沒有出現:D怪異af:D – lifeanddeath