0

I'm trying to check if server is available but when running this code I get error that server is not available or sometimes application freezes even server is properly running:

InetAddress in;
    in = null;

        try {
            in =  InetAddress.getByAddress(new byte[] { (byte)192, (byte)168, (byte)16, (byte)48});
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            if (in.isReachable(5000)) {
                loadProduct();
            } else
            {
                showAlertBox("Warning", "Server not available!");
            }
        } catch (IOException e) {
            showAlertBox("Warning", "Server not available!");
        } 

Is there any better way to check if server is online?

Josef
  • 2,648
  • 5
  • 37
  • 73
  • I have found simple solution at http://stackoverflow.com/a/15785326/1866833 Thanks to all. – Josef Jun 27 '13 at 06:36

3 Answers3

0

Is there any better way to check if server is online

Yes. Try to connect to it. That is an infallible test. Don't do it until you need to, of course. Don't try to predict the future. Just connect and handle the failure.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

I have tried with this

        Socket socket = null;
    boolean reachable = false;
    try {
        socket = new Socket("192.168.16.48", 80);
        reachable = true;
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        showAlertBox("Warning", "Server not available!");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        showAlertBox("Warning", "Server not available!");
    } finally {            
        if (socket != null) try { socket.close(); } catch(IOException e) { }
    }

It works, but sometimes I get meesage that that applications is not responding, even if socket timeout is adjusted.

Josef
  • 2,648
  • 5
  • 37
  • 73
  • I don't know why you tried with this when you had had eight hours to consider that you shouldn't try to predict the future. Just try to make the connection *when you need the connection,* and cope with the failure *at that time.* You have to do that anyway: why handle it twice? – user207421 Jun 27 '13 at 12:44
-1

You can try to locate the server using its host name. The advantage: unlike the dynamic ip address, the host name probably has a longer validity. In the following example, both network interfaces can refer to the same host.

byte[] b = { 10, 0, 0, 1 };

String name = "Hostname";

InetAddress ia = Inet4Address.getByAddress(b);
InetAddress ia2 = Inet4Address.getByName(name);

Another possible solution: before using network operations on android, you must get the corresponding permissions from the android manifest. I don't know exactly, which of the following are must-haves:

<uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.NETWORK_STATE" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Horschtele
  • 109
  • 3
  • 1
    (1) There's nothing here that answers the actual question. (2) There's nothing in the question about dynamic API addresses: in fact the question shows a *local* IP address. (3) There's nothing there about connectivity permission failures either. -1 – user207421 Jun 26 '13 at 23:01