05-09-2012 09:59 AM
I want to establish socket connection which will always listen to server.
I have used stream connetcion and followed socket demo provided in samples and implemeted keepalive by pinging server after every 1 minute.
When I send keepalive message the socket connection works fine but in my receiver thread i get exception as connection closed.
I am stcuk at this issue. Tried many different things but no luck.
On wi-fi everything works fine for me but as soon as i disable wi-fi the above mentioned issue occurs and my application fails to implement always connected feature.
Please help me guys.
05-11-2012 05:07 AM
I presume that you have been told that keeping a socket open over a wireless (non WiFi) network is a bad idea from the perspective of battery life and network charges? And of course on older phones or when a newer phone is not in a 3G area, a phone call will kill the connection anyway?
I would strongly suggest that you do whatever processing you are attempting another way, look at PUSH for example.
But getting back to your question.
I am not sure what you mean by ping. You processing will have to send and receive a message at least every 2 minutes (1 minute is a good value) to keep the socket open.
I have not tested this processing specifically on a WiFi connection. Perhaps because the WiFi processing is a full TCP Stack, it is not as critical for that, so I would not assume that code that works on WiFi would work on wireless.
You say "but in my receiver thread ". Is this your receive Thread on the Server?
Just one final thing. My socket connections are all half duplex, in other words I have one Thread running that handshakes with the Server and has a 'send' or 'receive' mode. I have never tested running a Socket connection on the BlackBerry with a send Thread and a receive Thread on the same connection.
05-28-2012 07:36 AM
Thanks for the reply.
"Ping" means after every one minute I send custom mesage to server so that if socket is disabled I can get the event of socket disconnection and reconnect the socket. Otherwise my reciever thread may block for read inputstream event.
I am using diiferent threads for sending and recieving coz its poosible that while sending some events to server, server sends command simultaneously.
05-28-2012 08:01 AM
"so that if socket is disabled I can get the event of socket disconnection and reconnect the socket"
The question then is why? if you need to send something to the Server, you can create a socket session. If the Server wants to send anything to you, it should use push (see Push API). What you are doing is not a recommended approach for the BlackBerry.
"I am using different threads for sending and receiving becuase it is possible that while sending some events to server, server sends command simultaneously."
You can still do this '1/2 duplex', if you start a second Thread, in other words have a send pipe and a receive pipe. It all depends on how you code the ends.
Personally my recommendation is that you swap to using Push. It is better for the BlacBberry (fewer connections), better for the battery life (less networking), better for the infrastructure (no 'ping' overhead) and you will find it is actually faster too.