views:

1454

answers:

5

Hi,

I am developing an application which connects to the server. By now the login and data transmission works fine if theserver is available. The problem arises when the server is unavailable. In this case the method sends a login request and waits for the response.

Does anyone know how to check if the server is available (visible)?

The pseudocode of the simple logic that has to be implemented is the following:

  1. String serverAddress = (Read value from configuration file) //already done
  2. boolean serverAvailable = (Check if the server serverAddress is available)//has to be implemented
  3. (Here comes the logic which depends on serverAvailable)
A: 

Send an ICMP ECHO REQUEST packet just like ping does.

See:--Good old Wikipedia

You should be able to clone most of the C code from the "ping.c" source of your favourite Linux distribution.

You can find the original ping source code here:- ping.c

James Anderson
+8  A: 

He probably needs Java code since he's working on Android. The Java equivalent -- which I believe works on Android -- should be:

InetAddress.getByName(host).isReachable(timeOut)
Sean Owen
+1  A: 

Are you working with HTTP? You could then set a timeout on your HTTP connection, as such:

private void setupHttpClient() {
    BasicHttpParams httpParams = new BasicHttpParams();

    ConnManagerParams.setTimeout(httpParams, CONNECTION_TIMEOUT);
    //...

    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(
            httpParams, schemeRegistry);
    this.httpClient = new DefaultHttpClient(cm, httpParams);
}

If you then execute a request, you will get an exception after the given timeout.

Matthias
A: 

Hi,

Oh, no no, the code in Java doesn't work: InetAddress.getByName("fr.yahoo.com").isReachable(200) although in the LogCat I saw its IP address (the same with 20000 ms of time out).

It seems that the use of the 'ping' command is convenient, for example:

Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("ping fr.yahoo.com -c 1"); // other servers, for example
proc.waitFor();
int exit = proc.exitValue();
if (exit == 0) { // normal exit
    /* get output content of executing the ping command and parse it
     * to decide if the server is reachable
     */
} else { // abnormal exit, so decide that the server is not reachable
    ...
}
kinhnc
A: 

hi,please help me, i tried to connect to sql server from android but i did not find anything ,i don't know what i must do ? i must use web service?

ameni