通过代码android模拟器获取pc的ip地址通过代码android

问题描述:

我想通过代码获取我的电脑在android模拟器的ip地址....或告诉我所有设备连接到一个局域网的ip地址以识别每个一个独特的.......请帮我理清这个问题提前通过代码android模拟器获取pc的ip地址通过代码android

+0

可能重复http://*.com/q/10118590/1160207 – 2012-04-13 06:23:39

上述功能仅是可能的检查IP地址将逐个添加的arp缓存每个人如何连接到设备。使用下面的代码并检查。只要把按钮与适当的名称,并调用此方法上点击

public void getClientList() { 

    int macCount = 0; 
    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) { 
       // Basic sanity check 
       String mac = splitted[3]; 

       if (mac.matches("..:..:..:..:..:..")) { 
        macCount++; 
        ClientList.add("Client(" + macCount + ")"); 
        IpAddr.add(splitted[0]); 
        HWAddr.add(splitted[3]); 
        Device.add(splitted[5]); 
        Toast.makeText(
          getApplicationContext(), 
          "Mac_Count " + macCount + " MAC_ADDRESS " 
            + mac, Toast.LENGTH_SHORT).show(); 
        for (int i = 0; i < splitted.length; i++) 
         System.out.println("Addressssssss  " 
           + splitted[i]); 
       } 
      } 
     } 
     // ClientList.remove(0); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      br.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

感谢您的帮助希亚姆,非常感谢 – 2012-04-13 06:42:45

+0

,你可以探索这个更多,如果我想识别在这个代码网络上的网络摄像头....我怎么能做到这一点 – 2012-04-13 06:44:10

+0

@AshishTamrakar - 嗨朋友,IP摄像头(如Linksys)得到了无线网络的能力(像其他设备),以便您可以直接连接通过wifi将相机传输到Android。在这种情况下,你可以使用IpAddr.add(splitted [0]);从上面的代码中获取摄像机的IP地址。但是如果摄像头是由另一台服务器(如拥有自己的软件的计算机)操作的,那么您需要进行套接字编程,以便从服务器获得响应,以获取连接到该服务器的所有设备(包括摄像头)的Ipaddress你的机器人。 – 2012-04-16 06:36:25

使用

感谢这段代码获取外部IP地址

 HttpClient httpclient = new DefaultHttpClient(); 
     HttpGet httpget = new HttpGet("http://api.externalip.net/ip/"); 
     HttpResponse response = null; 
     try 
     { 
     response = httpclient.execute(httpget); 
     } 
     catch (ClientProtocolException e) 
     { 
    e.printStackTrace(); 
     } 
     catch (IOException e) 
     { 
    e.printStackTrace(); 
     } 
     Log.e("",""+response); 
     HttpEntity entity = response.getEntity(); 
     if (entity != null) { 
     long len = entity.getContentLength(); 
     if (len != -1 && len < 1024) 
     { 
     try 
     { 
     str=EntityUtils.toString(entity); 
     Log.e("",""+str); 
     } 
     catch (ParseException e) 
     {    
    e.printStackTrace(); 
    } 
     catch (IOException e) 
     {     
    e.printStackTrace(); 
    } 
    } 
     }