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
Contributor
Posts: 42
Registered: ‎11-15-2010
My Device: Not Specified
Accepted Solution

Socket connection problem

I would like to create some application on Blackberry mobile phone. It uses TCP socket that connect to server. But my application will wait some data from the server. When the data is revieved, the InputStream will wait data again. So it work in the While loop. 

 

 

I have some question about that. It works find when I reconnect to server again and again like this.

 

 

while (alive) {

	connection =  	 (SocketConnection)Connector.open(URL,Connector.READ_WRITE, true);
        in = new InputStreamReader(connection.openInputStream());
	readData(in);
	in.close();
	connection.close();
}

But I do not want to create many connection in the loop. When I move the connection statement out of loop and remove the connection.close(); statement, the exception will occur like this "input stream already open"

 

I do not know how to solve that.

Help me please.

 

 

Thanks in advance

 

 

 

 

Developer
Posts: 402
Registered: ‎11-26-2010
My Device: Torch

Re: Socket connection problem

[ Edited ]

Easy. You have to move this line

 

 

in = new InputStreamReader(connection.openInputStream());

 

 

outside the loop so you won't try to open the input stream multiple times.

Your code should look something like this:

 

 

connection =(SocketConnection)Connector.open(URL,Connector.READ_WRITE, true);
in = new InputStreamReader(connection.openInputStream());
while (alive) {
	readData(in);
}

And after you exit the loop you should close the connection and the stream.

 

Let me know if this is the solution!

 

----------------------------------------------------------------------
Press the button to give kudos if I helped you Smiley Happy
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Socket connection problem

The normal processing with a socket involves two ends that exchange information.

 

So a Server will accept connections on the a port, a client will open a connection on a port. 

 

Once this connection is open, then the two can read and write on this connection. 

 

Once this has been done, the normal processing involves some sort of handshaking, so the client will send the server something (by using write), followed by an indicator that it has sent all the data for the moment and is expecting a response.  The server will read the input data, process it and do the reverse.  So the connection remains open, both ends read and write to it.

 

Exactly what the client and server do depends on how each end has been programmed, but normally there is some agreed protocol, that is specific to the Server typically. 

 

Now keeping a connection open is expensive in terms of batter life on the phone.  Also connections that are not busy are killed, so you have to have a 'heart beat' transmission on the socket connection if you want to keep it open. 

 

If you have a socket on the the BlackBerry that you are using to retrieve data from the Server, and this data arrives occasionally, you are much better using PUSH.

Highlighted
Contributor
Posts: 42
Registered: ‎11-15-2010
My Device: Not Specified

Re: Socket connection problem

[ Edited ]

Thank you anyone to suggest me. Now my project is work fine with  dx22's code.