sockets – Checking if server is online from Java code

sockets – Checking if server is online from Java code

The check method can be rewritten as follows (Java 7 and later):

public static boolean hostAvailabilityCheck() { 
    try (Socket s = new Socket(SERVER_ADDRESS, TCP_SERVER_PORT)) {
        return true;
    } catch (IOException ex) {
        /* ignore */
    }
    return false;
}

In addition to simplifying the exception handling, this eliminates a pesky Socket leak. (If you are concerned with the time taken to do this check, then set a connection timeout before attempting to connect: see Setting a timeout for socket operations)

But the problems with this approach are many-fold:

  • It only tests that something is listening for connections. If your service is behind a proxy … or is managed by something like the inetd service … then the accepted connections dont mean your service is actually working.

  • This is going to cause your service to see connections that close down without sending a request. So youd better code your service to deal with this gracefully.

  • Doing this repeatedly adds to network and server load.

  • If you set a short timeout because you dont want the test to freeze, then you risk setting it too short and judging the host to be down when it isnt.


After server is started I want to stop checking until the server changes status

That is next to impossible. The reason is that you wont be able to tell whether the server has changed status without checking. Or at least, you wont be able to do this without implementing an elaborate status notification service where the server calls the client to tell it is changing status. (And if change status includes die or lost network connection, then you wont be able to make that notification reliable … if at all.)

public static boolean hostAvailabilityCheck() { 
    try (Socket s = new Socket(SERVER_ADDRESS, TCP_SERVER_PORT)) {
        return true;
    } catch (IOException ex) {
        /* ignore */
    }
    return false;
}

working, but the problem is that when you turn on the phone throught WI-FI it comes to a screeching halt and no action. for thought…=)

next code will be to work through WI-FI … if you increase the connection time –

public static boolean isOnline() {
    boolean b = true;
    try{
        InetSocketAddress sa = new InetSocketAddress(SERVER_IP_ADDRESS, PORT);
        Socket ss = new Socket();
        ss.connect(sa, 1);            --> change from 1 to 500 (for example)
        ss.close();
    }catch(Exception e) {
        b = false;
    }
    return b;
}

sockets – Checking if server is online from Java code

First check if server is running and the server accepts the connection.

   public static boolean hostAvailabilityCheck()
{ 
    s = new Socket(SERVER_ADDRESS, TCP_SERVER_PORT);
    boolean available = true; 
    try {               
        if (s.isConnected())
        { s.close();    
        }               
        } 
    catch (UnknownHostException e) 
        { // unknown host 
        available = false;
        s = null;
        } 
    catch (IOException e) { // io exception, service probably not running 
        available = false;
        s = null;
        } 
    catch (NullPointerException e) {
        available = false;
        s=null;
    }


    return available;   
} 

Leave a Reply

Your email address will not be published. Required fields are marked *