How do I get list of IP addresses for devices connected to my same subnet using Java?
How do I get list of IP addresses for devices connected to my same subnet using Java?
I saw the other answer. Its pretty well. But its quite slow as compared to a real situation and should not be used in such a senario.
Here is what i am talking about.
When i use the answer https://stackoverflow.com/a/3345981/14911094
Code:
import java.net.InetAddress;
public class Main {
public static void main(String[] args) throws Exception{
long initialT = System.currentTimeMillis();
checkHosts("192.168.0");
long finalT = System.currentTimeMillis();
System.out.println("Scan Completed taking " + (finalT - initialT) + " miliseconds approximately!");
}
public static void checkHosts(String subnet) throws Exception{
int timeout=1000;
for (int i=1;i<255;i++){
String host=subnet + "." + i;
if (InetAddress.getByName(host).isReachable(timeout)){
System.out.println(host + " is reachable");
}
}
}
}
The Output:
sudo java Main
[sudo] password for jaysmito:
192.168.0.1 is reachable
192.168.0.2 is reachable
192.168.0.3 is reachable
192.168.0.4 is reachable
192.168.0.10 is reachable
Scan Completed taking 250151 miliseconds approximately!
This is pretty slow but i tried to make a better version using this concept:
Code:
import java.net.*;
import java.io.*;
import java.util.*;
class AddressFinderLevel4 extends Thread{
private String addmask;
private Stack<String> stack;
private int start, end;
public AddressFinderLevel4(String addmask, Stack stack, int start, int end){
this.addmask = addmask;
this.stack = stack;
this.start = start;
this.end = end;
}
@Override
public void run(){
try{
int timeout=1000;
for(int i = start; i <= end; i++){
String host=addmask + "." + i;
if (InetAddress.getByName(host).isReachable(timeout)){
stack.push(host);
}
}
}catch(Exception ex){
}
}
}
class AddressFinderLevel3 extends Thread{
private String addmask;
private Stack<String> stack;
private int start, end;
private int packSize;
public AddressFinderLevel3(String addmask, Stack stack, int packSize, int start, int end){
this.addmask = addmask;
this.stack = stack;
this.start = start;
this.end = end;
this.packSize = packSize;
}
@Override
public void run(){
try{
for(int i = start; i <= end; i++){
int j = 1;
String host = addmask + "." + i;
while(j<=255){
AddressFinderLevel4 addressFinderLevel4 = new AddressFinderLevel4(host, stack, j, j+packSize+5);
addressFinderLevel4.start();
j = j + packSize;
}
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
public class Main {
public static void main(String[] args) throws Exception {
System.out.println("Starting search!");
Stack data = find();
Thread.sleep(1000);
System.out.println("Data found in 1000 miliseconds");
data.forEach(System.out::println);
Thread.sleep(10000);
System.out.println("Data found in 10000 miliseconds");
data.forEach(System.out::println);
Thread.sleep(25000);
System.out.println("Data found in 25000 miliseconds");
data.forEach(System.out::println);
}
public static Stack find(){
Stack<String> stack = new Stack<String>();
AddressFinderLevel3 finder = new AddressFinderLevel3("192.168", stack, 10, 0, 255);
finder.start();
return stack;
}
}
The output:
sudo java Main
Starting search!
Data found in 1000 miliseconds
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
Data found in 10000 miliseconds
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.99.152
192.168.0.10
192.168.102.227
192.168.99.161
Data found in 25000 miliseconds
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.99.152
192.168.0.10
192.168.102.227
192.168.99.161
As you can see this scans far more IPs in much much less time.
I had done it for all possible ips too but that takes too much memory and is not needed!
This is a faster solution thus should perform better!
None of these were working for me as I had created the server and it didn't respond to these pings. So I created a mechanism that makes the server reply to a ping and used the brute force checking method for all addresses in the subnet.
PS: Writing this because someone creating their own server might need this.