Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find server IP in local network with known port in Java/Android

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!

like image 343
rtc11 Avatar asked Oct 19 '12 21:10

rtc11


2 Answers

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.

like image 86
Bert Avatar answered Nov 10 '22 08:11

Bert


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

https://github.com/angryziber/ipscan

like image 2
Nesim Razon Avatar answered Nov 10 '22 07:11

Nesim Razon