Im trying to check if an IP on a local network has a given PORT open. To do this I iterate over all the IP in the subnet. This is what my code looks like:
for(int i = 0; i<256; i++){
Socket kkSocket = new Socket();
try{
kkSocket.bind(null);
InetSocketAddress temp = new InetSocketAddress("10.0.0.", PORT);
kkSocket.connect(temp, 10);
Log.i("Scan", "found " + temp.getHostName());
}catch(Exception e){ }
}
This works perfectly on my computer, but when I try to run it on my Android device, it wont work. Ive tried to delay it with Thread.sleep(100), kkSocket.close() and kkSocket.keepAlive(false) but nothing seems to work. It works if I DONT iterate and just try to connect to 10.0.0.45 (which is the correct IP) but thats hacking (im not supposed to know which IP to test). Does anyone know what cause my problem or how to fix it?
Shouldn’t this line:
be: