Can someone tell me how to find the IP addrss of all the systems connected to same network in Android programatically. DHCPInfo class gives only the ipaddress assigned to our android device but not for the other devices connected to the same network. How to get ipaddress of other devices connected to same network?
Asked
Active
Viewed 1.3k times
3
-
I do not think a DHCP server is going to provide this information to one of its (connected) clients independent of the platform (Android in this case) of the client... – Veger Apr 16 '13 at 11:19
-
Since the IP V4 address range is pretty much used up by now, simply assume that every valid non-local IP V4 address is used by a device on the same network, namely the Internet. You'll be able to live with the few exceptions. -- Sorry, couldn't resist. You're the n-th person asking this during the last few days. Of course, this is impossible in general. – class stacker Apr 16 '13 at 11:19
-
So clever @Class Stacker – fweigl Apr 16 '13 at 11:22
-
http://stackoverflow.com/questions/8455755/network-discovery-in-java-multicast-broadcast-java – Triode Apr 16 '13 at 11:22
-
@RajeshCP Unlike the post you refer to, this question here is Android related, and the OP did not indicate that the devices run a particular app. Besides, the question is whether or not you have a cooperative infrastructure. In many places, the router of "the network" will disallow communication between network clients. – class stacker Apr 16 '13 at 11:25
-
@ClassStacker http://stackoverflow.com/questions/9504721/getting-ip-addresses-of-the-pcs-available-on-wifi-network-in-android I got that reference from this link. This is pretty much related to android. – Triode Apr 16 '13 at 11:28
-
@RajeshCP Again, the solution in the second link you posted requires cooperative software which voluntarily communicates, either P2P or client-server. Whereas the original poster simply wants to find the IP addresses of all _systems_ (!) connected to the same network (presumably he means the smallest common subnet but we can't be sure). So these solutions do not match the question. – class stacker Apr 16 '13 at 11:34
-
@ClassStacker You may be missing the point here. Connect to your router's admin interface - and it will have an option to see all devices on the local network. The OP doesn't care about the internet, but only local subnet. If his subnet mask is 255.255.255.0 then he needs to know which of the 255 addresses are in use. – Aleks G Apr 16 '13 at 15:01
-
@AleksG Admittedly, I was in a cheeky mood. I have configured quite a few routers. The point is, the OP wants to _find the IP addresses of all the systems connected to same network_. He does not mention a friendly environment, such as the router being under his control or all other systems being able to run a certain app. Hence my claim that _this is impossible in general_. – class stacker Apr 16 '13 at 19:27
2 Answers
5
First get the host ip by this
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 is host ip
Now get connected ips by this
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);
intToIp
public String intToIp(int i) {
return (i & 0xFF) + "." +
((i >> 8 ) & 0xFF) + "." +
((i >> 16) & 0xFF) + "." +
((i >> 24) & 0xFF);
}

keshav
- 3,235
- 1
- 16
- 22
-
-
-
-
Hello. After run your provice code, i only see my device IP is response to ping, even i know there are mores than one devices connect to my wifi... – kemdo Apr 17 '18 at 17:31
-1
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());
}
}
}
}

Veger
- 37,240
- 11
- 105
- 116

Sunil Kumar
- 7,086
- 4
- 32
- 50
-
From what I see, this just lists the addresses of local interfaces on the Android device. The OP wants the list of other devices connected to the local network. Your code has nothing to do with the question. – Aleks G Apr 16 '13 at 15:14