1

I'm trying to see what IP's on a list are actually online. I'm currently using

    public boolean IsOnline(String ip) { 
    try (Socket s = new Socket(ip, 80)) {
        return true;
    } catch (IOException ex) {
    }
    return false;
}

To check if the server is online. But it takes about 30 seconds for each IP to get checked. Is there a way to get faster results like another kind of method?

Sam
  • 7,252
  • 16
  • 46
  • 65

3 Answers3

0

Not sure, but would INetAddress.isReachable suffice for your needs? You can't do ICMP messages in Java.

M. le Rutte
  • 3,525
  • 3
  • 18
  • 31
0

You may use the INetAddress.isReachable

".. A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host..".

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

The only reason I can imagine, why this takes 30 seconds is, that nobody listens to port 80 and your socket connection timeout is 30 seconds. Please check for this by adding ex.printStackTrace() within the catch clause.

Please take a read here: Why are empty catch blocks a bad idea?

How you should "ping" a server, depends on what ports it is listening to. Normally INetAdresse.isReachable() works good. If your administrator blocks ICMP requests you may use the ssh port 22, to probe for the servers existence.

Community
  • 1
  • 1
cruftex
  • 5,545
  • 2
  • 20
  • 36