0

How to check if server is online or offline, and if is offline start connecting until server is on. I have tried with this:

        connectBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            new Thread(rConnection).start();

        }
    });

   public Runnable rConnection = new Runnable() {

    @Override
    public void run() { 
        boolean status = connect();         
        while (!status)
        {
            System.out.println("Connection Status: " + status);
            status = Connect();
        }
    }
 };

public boolean Connect() {

        boolean status = false;

        try {
            s = new Socket(SERVER_ADDRESS, TCP_SERVER_PORT);

            System.out.println("Socket: " + s.toString());

            if (s.toString() != "")
            {
                status = true;
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
            status = false;
            s=null;
        } catch (IOException e) {
            e.printStackTrace();
            status = false;
            s=null;
        } catch (NullPointerException e)
        {
            e.printStackTrace();
            status = false;
            s=null;
        }

        return status;
    }

If server is running before staring app it connects successfully but if server is off or disconnects after some time I don't get any error message and it won't start reconnecting again. How to solve this?

Michael Shrestha
  • 2,547
  • 19
  • 30
Josef
  • 2,648
  • 5
  • 37
  • 73

3 Answers3

1

Basically you may split this:

s = new Socket(SERVER_ADDRESS, TCP_SERVER_PORT);

into

s = new Socket();
s.connect(remoteAddr,timeout)

And then control if connect returns on timeout or on successfull connection.

PeterMmm
  • 24,152
  • 13
  • 73
  • 111
  • InetSocketAddress socketAddress = new InetSocketAddress(SERVER_ADDRESS, TCP_SERVER_PORT); s.connect(socketAddress, 2000); Now I get Null Pointer Exception even server app is started and listening? Do you know why? – Josef Jun 19 '13 at 10:57
  • 1
    `s`is defined (not null) ? Any stack trace ? – PeterMmm Jun 19 '13 at 11:06
  • 1
    That code won't throw a `NullPointerException` unless `s` is null. The only explanation is that you left out the `= new Socket()` part of the code. – user207421 Jun 19 '13 at 11:17
  • Sorry, my mistake. Now it works. Thanks. Now I'm facing with problem. If there is no any error I need to stop checking connection so I have a global variable status. You can see it in code that i posted. Changing the state variable doesn't stop thread to check connection. Could you explain me why? – Josef Jun 19 '13 at 11:20
0

Look at this thread for a solution and keywords: How can I monitor the network connection status in Android? . Also, consider retrying requests on a new connection if the underlying connection is lost (or times out).

Community
  • 1
  • 1
Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
  • This isn't that I want because server app is running on specific port and IP address. – Josef Jun 19 '13 at 09:59
  • Try applying [this](http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html) using [this](http://developer.android.com/reference/java/net/SocketOptions.html#SO_KEEPALIVE). This will cause a bit more network traffic, but you will be able to restart the connection immediately (if you have connectivity). – Tassos Bassoukos Jun 19 '13 at 10:17
0

How to check if server is online or offline, and if is offline start connecting until server is on

Try to connect to it when you need to connect to it, and handle the failures that result. At present you seem to be trying to maintain an eternal connection, which is never going to work. The best way to detect whether a resource is available is to try to use it at the time that you need to use it. Anything is subject to numerous sources of error such as timing window problems, testing the wrong thing, testing the right thing at the wrong time, and at best to overuse of scarce resources. Rethink your requirement.

user207421
  • 305,947
  • 44
  • 307
  • 483