Reputation: 11
I am developing an app, in which I have maintained a infinite socket connection with server. It works pretty well all the time except when device stays in idle mode for longer time say 30 mins or so. After 30 mins if I bring device to wake state, and try to contact to server thro' my app it doesn't throw any exception. Which shows me that my socket connection is in still live state. But when I check at the server end same data is not received here. So I am not able to understand that is there is a known issue on android side or if I am doing anything wrong here. Below is the code piece I am using to setup the socket.
Socket socket = new Socket(url, port);
String request = "some request";
outputStream = socket.getOutputStream();
outputStream.write(request.getBytes());
outputStream.flush();
InputStream is = socket.getInputStream();
while (true) {
StringBuffer sb = new StringBuffer();
int ch = is.read();
while (ch != -1) {
sb.append((char) ch);
ch = is.read();
}
}
Upvotes: 1
Views: 1284
Reputation: 1502
You need to send application level keep alives to maintain your socket connection. Here is an example: http://devtcg.blogspot.com/2009/01/push-services-implementing-persistent.html
Upvotes: 1
Reputation: 2421
Even though it does not directly answer your question but ,I would recommend that you use apache HTTP client api for persistant connection managent.
Here is the link of their tutorial ,specifically connection persistence.
Upvotes: 0