0

I am wondering what is the proper way to check on the client side that a TCP socket opened using the AndroidAsync library is no longer available? This is in the case the (plain TCP, non-AndroidAsync) server did not initiate explicitly closing the socket (so the ClosedCallback is not invoked). For instance, when the server has been cold rebooted.

It seems that the DataCallback is available only when the server sends back data and can't be used to receive error messages.

It seems to me also that

Util.writeAll(socket, (byte[]) payload.array(), new CompletedCallback()
    {
        @Override
        public void onCompleted(Exception ex)
        {
            if (ex != null)
            {
                Log.e(TAG, "write failed with ex message= " + ex.getMessage());
                throw new RuntimeException(ex);
            }
        }
    });

does not throw an Exception either.

So at this point I'm not sure how to detect the socket is no longer available even if the client periodically writes data to it.

3 Answers 3

0

It will throw an IOexception if you send enough data or call it enough times. It won't throw on the first call due to TCP buffering at both ends.

1
  • That makes sense. However, the client is a remote controller to the server which has a visual front end. By the time "enough" data/calls are sent, the two would be long out of sync causing a bad user experience.
    – Objectist
    May 11, 2016 at 21:55
0

I ended up implementing some sort of a "ping"-alike periodic check. The client opens and immediately closes a TCP connection to the very same port using a plain Java NIO socket call (not using AndroidAsync). If that one times out, it is assumed that the connection has been lost, and a recovery attempt is made once it succeeds again. This periodic check is performed only when the app has focus, or is just awakened. This is clearly a far from ideal workaround but it seems to work for my purposes.

-1

You could use the closed/end callbacks

socket.setClosedCallback(new CompletedCallback()
{
            @Override
            public void onCompleted(Exception ex)
            {

            }
        });

socket.setEndCallback(new CompletedCallback()
{
            @Override
            public void onCompleted(Exception ex)
            {

            }
 });
3
  • Unfortunately the server is not an AndroidAsync server and won't close the connection so that the callbacks could be invoked.
    – Objectist
    May 11, 2016 at 21:52
  • Sorry, what do you mean by non-AndroidAsync server? Even with a node.js, or even a custom c++ tcp server, I still get the end and closed callbacks on the client when the server dies, I don't think it has anything to do with AndroidAsync and it is more of a tcp standard
    – behelit
    May 12, 2016 at 5:09
  • In my environment there are other use cases besides shutting down the server (gracefully) when the connection is no longer valid from the viewpoint of both the client and the server. Because the server is not an AndroidAsync server implemented/controlled by me that has to be discovered purely on the client's side.
    – Objectist
    May 24, 2016 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.