Maro
Maro

Reputation: 253

Android, holding tcp connection in sleep mode

I am trying to keep a tcp connection to a server alive even while the phone goes into sleep mode. I have searched everywhere and tried everything. This problem occurs on some phones and not others which is kind of random.

So basically one client sends a request to the server, then the server sends the request to another client. What happens is that the receiving client doesn't get the request at all. I have tested this with a debugger and the next line of code after the read never gets called. It is important for the device to receive the message right away. I am wondering how viber is achieving this. I thought about google cloud messaging but i would have to re-implement a lot, also according to the documentation, even with google cloud messaging the message doesn't necessarily reach the destination right away.

here is my code:

 class BackgroundReadThread extends Thread {

    @Override
    public void run()
    {
        while(connectedToServer)
        {
            try
            {
                int bytesRead=0;
                if(myWifiLock!=null && !myWifiLock.isHeld())
                    myWifiLock.acquire();
                byte val=(byte)myInputStream.read();
                myWakeLock.acquire();//this line never gets called when in sleep
                if(val==-1)
                {
                    unexpectedDisconnectionFromServer();
                    if(myWifiLock!=null && myWifiLock.isHeld())
                        myWifiLock.release();
                    myWakeLock.release();
                    return;
                }
                bytesRead=myInputStream.read(myBuffer, 0, bufferSize);
                if(bytesRead<1)
                {
                    unexpectedDisconnectionFromServer();
                    if(myWifiLock!=null && myWifiLock.isHeld())
                        myWifiLock.release();
                    myWakeLock.release();
                    return;
                }
                byte[] dataArray=Arrays.copyOfRange(myBuffer,0,bytesRead);
                ByteBuffer data=ByteBuffer.allocate(bytesRead+1).put(val).put(dataArray);
                myParent.invokeReceiveAction(data, bytesRead+1);
            }
            catch (IOException e)
            {
                myWakeLock.acquire();
                unexpectedDisconnectionFromServer();
                e.printStackTrace();
            }
            finally
            {
                if(myWifiLock!=null && myWifiLock.isHeld())
                    myWifiLock.release();
                if(myWakeLock!=null && myWakeLock.isHeld())
                    myWakeLock.release();
            }
        }
    }
}

EDIT: forgot to mention that this code is running in a service

Upvotes: 3

Views: 1600

Answers (1)

Maro
Maro

Reputation: 253

I have no idea why but the problem only occurs sometimes and it only occurs on the debug version of the application. I have tested the release version of the application and it never failed once on any of the phones ive tested it on. So i guess the problem is with the debug version although i have no idea why. Hope this helps someone having similar problems.

Upvotes: 1

Related Questions