2013-04-16 49 views
3

有人可以告訴我如何在程序中找到連接到同一網絡的所有系統的IP地址。 DHCPInfo類僅提供分配給我們的android設備的IP地址,但不提供給連接到同一網絡的其他設備。如何獲得連接到同一網絡的其他設備的IP地址?如何獲得連接到同一個Wi-Fi網絡的系統的所有IP地址android

+0

我不認爲一個DHCP服務器不將向客戶端提供這些信息給獨立於客戶端的平臺(在這種情況下是Android)... – Veger

+0

由於IP V4地址範圍現在已經非常耗盡,只需假設每個有效的非本地IP V4地址都由同一網絡(即Internet)上的設備使用。除了少數例外,你將能夠生活。 - 抱歉,無法抗拒。在過去的幾天裏,你是第n個問這個問題的人。當然,這是不可能的。 –

+0

所以聰明的@Class堆棧器 – FWeigl

回答

0
private class Task extends AsyncTask<void void="">{ 

    InetAddress[] inetAddress = null; 
    List<string> hostList = new ArrayList<string>(); 

    @Override 
    protected Void doInBackground(Void... arg0) { 

    doTest(); 
    } 


    @Override 
    protected void onPostExecute(Void result) { 

    ArrayAdapter<string> adapter 
    = new ArrayAdapter<string>(
     AndroidInetActivity.this, 
     android.R.layout.simple_list_item_1, 
     hostList); 

    resultList.setAdapter(adapter); 
    } 

    private void doTest(){ 

     String host = hostinput.getText().toString(); 
    inetAddress = InetAddress.getAllByName(host); 

    for(int i = 0; i < inetAddress.length; i++){ 

    hostList.add(inetAddress[i].getClass() + " -\n" 
     + inetAddress[i].getHostName() + "\n" 
     + inetAddress[i].getHostAddress()); 
     } 

    } 
    } 
} 
+0

從我看到的情況來看,這只是列出了Android設備上本地接口的地址。 OP想要連接到本地網絡的其他設備的列表。你的代碼與這個問題無關。 –

5

首先通過這個

public String s_dns1 ; 
    public String s_dns2;  
    public String s_gateway; 
    public String s_ipAddress;  
    public String s_leaseDuration;  
    public String s_netmask; 
    public String s_serverAddress; 
    TextView info; 
    DhcpInfo d; 
    WifiManager wifii; 

    wifii = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
     d = wifii.getDhcpInfo(); 

     s_dns1 = "DNS 1: " + String.valueOf(d.dns1); 
     s_dns2 = "DNS 2: " + String.valueOf(d.dns2); 
     s_gateway = "Default Gateway: " + String.valueOf(d.gateway); 
     s_ipAddress = "IP Address: " + String.valueOf(d.ipAddress); 
     s_leaseDuration = "Lease Time: " + String.valueOf(d.leaseDuration); 
     s_netmask = "Subnet Mask: " + String.valueOf(d.netmask); 
     s_serverAddress = "Server IP: " + String.valueOf(d.serverAddress); 

d.dns1得到該主機的IP主機IP

現在得到連接IPS本

String connections = ""; 
     InetAddress host; 
     try 
     { 
      host = InetAddress.getByName(intToIp(d.dns1)); 
      byte[] ip = host.getAddress(); 

      for(int i = 1; i <= 254; i++) 
      { 
       ip[3] = (byte) i; 
       InetAddress address = InetAddress.getByAddress(ip); 
       if(address.isReachable(100)) 
       { 
        System.out.println(address + " machine is turned on and can be pinged"); 
        connections+= address+"\n"; 
       } 
       else if(!address.getHostAddress().equals(address.getHostName())) 
       { 
        System.out.println(address + " machine is known in a DNS lookup"); 
       } 

      } 
     } 
     catch(UnknownHostException e1) 
     { 
      e1.printStackTrace(); 
     } 
     catch(IOException e) 
     { 
      e.printStackTrace(); 
     } 
     System.out.println(connections); 
+0

真的很棒,我們怎樣才能得到每個IP地址的主機名? –

相關問題