Find the IP address of a server on a local network with a known port in Java / Android

I want to find the IP address on a server on the local network in a short time. I know the port that the application uses on the server.

I tried this, but its too slow. Even when I know the IP, the respiration is too long (for example, about 4 seconds for each IP). Given this method, it would take minutes to scan the entire subnet from 10.0.0.0 to 10.0.0.255.

String ip = "10.0.0.45"; try { InetAddress ping = InetAddress.getByName(ip); Socket s = new Socket(ping, 32400); System.out.println("Server found on IP: " + ping.getCanonicalHostName()); s.close(); } catch (IOException e) { System.out.println("Nothing"); } } 

I could use streams, but that would still be slow. I saw the application detect IP in milliseconds. How do they do it? Java code would be appreciated!

+4
source share
2 answers

I can suggest looking for the source code of an angry ip scanner. I think it's fast enough.

https://github.com/angryziber/ipscan

+2
source

You need to do two things - use threads to check many hosts at the same time and give the socket a lower timeout.

This answer shows a very similar example.

+3
source

Source: https://habr.com/ru/post/1440882/


All Articles