Problem:
I'm making an app that has instant messaging. For the most part, the messages are sent and received without any issues. But after some period of time, somewhere between 10 and 40 minutes, the messages no longer reach the server if the client's socket hasn't been used in a while. For example, if I chat with someone, leave the app open, take a nap, and come back to chat again, then the messages won't send. I am 99% sure it is not a problem on the server side because I print out everything it receives (and it's not receiving anything) and it works fine if I log in with another phone.
What I've tried:
At first I thought it was a timeout issue on the client side because on the server side I kept getting an error saying, "Connection reset by peer", somewhere in that inverval of 10-40 minutes. I (almost) resolved this by using Java.net.socket
's connect()
method with a timeout value of 0 which gives an infinite timeout:
clientSocket = new Socket();
clientSocket.connect( new InetSocketAddress(ServerInfo.IP, ServerInfo.PORT_NUMBER), 0 );
I rarely get the "Connection reset by peer" error message now, but the mysterious problem of messages not sending after some time goes by still remains.
Code:
This is my sendMessage
function which always logs "SOCKET OPERATOR SENDING MESSAGE: 'message'":
public boolean sendMessage(String message)
{
PrintWriter out = null;
try
{
out = new PrintWriter( clientSocket.getOutputStream(), true );
Log.i( "MY_TAG", "SOCKET OPERATOR SENDING MESSAGE: " + message );
}
catch (IOException e)
{
e.printStackTrace();
Log.i( "MY_TAG", "SOCKET OPERATOR FAILEDD TO SEND MESSAGE WITH EXCEPTION: " + e.getMessage() );
return false;
}
out.println(message);
return true;
}
Question:
What on earth could possibly be the problem or how could I further debug this?