我想创建一个简单的"将在给定网络中找到所有IP地址的应用程序..
例如......
我有一个按钮,点击后,找到所有IP地址并在ListView中显示..
有没有人有我可以解释如何做的示例或功能源代码?
抱歉我的英文=)
答案 0 :(得分:3)
这是我在本地网络中查找IP地址的代码。
首先,您必须找到您的设备IP地址,然后使用该IP地址的subnet
ping每个IP地址。假设设备IP地址为192.168.0.100
,则本地subnet
为192.168.0.
。然后将每个IP地址从192.168.0.1
ping到192.168.0.255
以查找设备的IP地址,您可以使用两种方法:
1方法:
String ip = "";
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
while(enumNetworkInterfaces.hasMoreElements())
{
NetworkInterface networkInterface = enumNetworkInterfaces.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface.getInetAddresses();
while(enumInetAddress.hasMoreElements())
{
InetAddress inetAddress = enumInetAddress.nextElement();
String ipAddress = "";
if(inetAddress.isSiteLocalAddress())
{
ipAddress = "SiteLocalAddress: ";
}
ip += ipAddress + inetAddress.getHostAddress() + "\n";
String subnet = getSubnetAddress(ip);
private String getSubnetAddress(int address)
{
String ipString = String.format(
"%d.%d.%d",
(address & 0xff),
(address >> 8 & 0xff),
(address >> 16 & 0xff));
return ipString;
}
2方法:
WifiManager mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo mWifiInfo = mWifiManager.getConnectionInfo();
String subnet = getSubnetAddress(mWifiManager.getDhcpInfo().gateway);
找到子网地址后,ping网络中的每个IP地址:
private void checkHosts(String subnet)
{
try
{
int timeout=5;
for (int i=1;i<255;i++)
{
String host=subnet + "." + i;
if (InetAddress.getByName(host).isReachable(timeout))
{
Log.d(TAG, "checkHosts() :: "+host + " is reachable");
}
}
}
catch (UnknownHostException e)
{
Log.d(TAG, "checkHosts() :: UnknownHostException e : "+e);
e.printStackTrace();
}
catch (IOException e)
{
Log.d(TAG, "checkHosts() :: IOException e : "+e);
e.printStackTrace();
}
}
ping每个IP地址后,android内核文件存储了网络中的那些IP地址。您可以通过调用AndroidOS文件获取此列表。此文件为arp
,存储在android中的/proc/net/
中。您可以通过代码进入。只需以编程方式执行特定命令并存储在modelData中,然后通过适配器通知listView:
ArrayList<IpAddress> mIpAddressesList;
private boolean getIpFromArpCache()
{
BufferedReader br = null;
char buffer[] = new char[65000];
String currentLine;
try
{
br = new BufferedReader(new FileReader("/proc/net/arp"));
while ((currentLine = br.readLine()) != null)
{
Log.d(TAG, "getIpFromArpCache() :: "+ currentLine);
String[] splitted = currentLine.split(" +");
if (splitted != null && splitted.length >= 4)
{
String ip = splitted[0];
String mac = splitted[3];
if (!splitted[3].equals(emptyMac))
{
if (!splitted[0].equals(IP))
{
// int remove = mac.lastIndexOf(':');
// mac = mac.substring(0,remove) + mac.substring(remove+1);
mac = mac.replace(":", "");
Log.i(TAG, "getIpFromArpCache() :: ip : "+ip+" mac : "+mac);
mIpAddressesList.add(new IpAddress(ip, mac));
}
}
}
}
return true;
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return false;
}
public class IpAddress
{
private String ipAddressName;
private String macAddress;
public IpAddress(String ipAddressName, String macAddress)
{
setIpAddressName(ipAddressName);
setMacAddress(macAddress);
}
public void setIpAddressName(String ipAddressName)
{
this.ipAddressName = ipAddressName;
}
public String getIpAddressName()
{
return this.ipAddressName;
}
public void setMacAddress(String macAddress)
{
this.macAddress = macAddress;
}
public String getMacAddress()
{
return this.macAddress;
}
}
但是对于在主线程上执行这些联网操作并不好。你必须在线程上推送所有代码。并在后台线程上执行所有网络相关的isses。希望它能帮到你。让我知道它的工作与否。
如果您需要,我想分享此演示的.apk
。
答案 1 :(得分:0)
你可以试试这样的:
public class MainActivity extends Activity {
WifiManager wifi;
String wifis[];
WifiScanReceiver wifiReciever;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
wifiReciever = new WifiScanReceiver();
wifi.startScan();
}
protected void onResume() {
registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
private class WifiScanReceiver extends BroadcastReceiver{
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = wifi.getScanResults();
wifis = new String[wifiScanList.size()];
for(int i = 0; i < wifiScanList.size(); i++){
wifis[i] = ((wifiScanList.get(i)).toString());
}
//here you can set your list view with wifis
}
}
}