6

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

I've tried this, but its too slow. Even when I know the IP, the responstime is too long (like 4 seconds or so for each IP). Considered this method, it would take minutes to scan the whole 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 threads, but that would still be slow. Ive seen application finding the IP in milliseconds out there. How do they do this? Java code would be appreciated!

rtc11
  • 747
  • 8
  • 23

2 Answers2

3

You'll want to do two things - use threads to check many hosts simultaneously, and give the socket connection a lower timeout.

This answer show a very similar example.

Community
  • 1
  • 1
Bert
  • 2,134
  • 19
  • 19
2

I can suggest to look for source code of angry ip scanner. It is fast enough I think.

https://github.com/angryziber/ipscan

Nesim Razon
  • 9,684
  • 3
  • 36
  • 48