Connection test

I have an application that will send some packets to the server.

I would like to before sending, check if the server is available. And for this I did the following:

   public static boolean isConnect(){

        boolean isOn = false;
        try{
            final String command = "/system/bin/ping -c 1 "+HOST;
            int wait = Runtime.getRuntime().exec(command).waitFor();
            Log.d(TAG,"wait: "+wait);
            isOn = ( wait == 0);
        }catch (final Exception e){}
        Log.d(TAG,"Connect: "+isOn);
        return isOn;
    }

But in all cases it always returns 1.

According to the documentation:

The exit value of the native process being waited on.

This may vary, if I am using 3G?

Or what is the best way to do this test?

I don't care if I have an internet connection, but if the HOST is responding!

Author: UzumakiArtanis, 2015-12-10

1 answers

Actually the method works correctly, the problem is that HOST does not accept ping!

 0
Author: Thiago Luiz Domacoski, 2015-12-15 12:13:14