If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
PayalBalodta
Posts: 3
Registered: ‎09-20-2011
My Device: Torch
My Carrier: Vodafone

Socket connection closed exception

Hi All,

     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.

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Socket connection closed exception

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. 

Please use plain text.
New Contributor
PayalBalodta
Posts: 3
Registered: ‎09-20-2011
My Device: Torch
My Carrier: Vodafone

Re: Socket connection closed exception

Hi Peter,

      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.

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Socket connection closed exception

"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. 

Please use plain text.