Anyone could you please help me to list the name of all computers connected to the LAN with JAVA?
Asked
Active
Viewed 1,668 times
0
-
2http://stackoverflow.com/questions/1617942/how-to-get-all-the-system-names-connected-to-lan-using-java – scrappedcola Oct 05 '12 at 18:51
-
@scrappedcola That question is only for Windows networks and uses jcifs. – Jim Garrison Oct 05 '12 at 19:47
1 Answers
1
One way you could solve this without using outside system calls would be to try every single possible IP address.
Iterate over addresses, sending data to each one using something like this...
for (int i =0; i<100; i++) {
String ip = "192.168.1." + i
InetAddress address = InetAddress.getByAddress(ip);
}
Any responses you get would indicate an active IP. You will be a limited by your subnet in this approach, however.

Chris Laplante
- 29,338
- 17
- 103
- 134

Max
- 5,799
- 3
- 20
- 27
-
This is the only way I can think of. I don't believe the Inet class lets you scan a network. No reason this wouldn't work. – Max Oct 05 '12 at 19:39
-
@EdwardJones - not without some additional knowledge of the network graph. If for instance you were running from the DHCP / DNS server, you might be able to query that to know what machines have allocated IPs, but if you're just a normal machine on the network, you're pretty much limited to polling all IPs in the subnet. – dimo414 Oct 05 '12 at 19:47
-
1There is no other way to enumerate reachable hosts on a network other than trying to connect to every address. Even that won't find everything, as some hosts may not respond to ICMP or whatever port you connect to. Remember to adjust the range you search based on your netmask. The code provided by @Max is incorrect in that it assumes a netmask of /24 and doesn't try all possible hosts in that range. – Jim Garrison Oct 05 '12 at 19:50
-
@JimGarrison Yeah, I pointed out in my last statement that he would be limited by the mask in this approach. Depending on what he needed to do, it could work for him though. – Max Oct 05 '12 at 19:55
-
In theory one could ping to the broadcast address (192.168.1.255), but in practice several operating systems won't respond to broadcast pings for security matters. So I guess this is as good as it gets. – pmoleri Oct 05 '12 at 20:13