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
Highlighted
Developer
Posts: 47
Registered: ‎05-06-2009
My Device: Not Specified
Accepted Solution

another socket problem: socket is not receiving although it is ready

[ Edited ]

Hi there!

 

I've read a lot of threads concerning sockets but I was not able to find a working solution:

I know RIM changed a lot of things regarding this in the OS 5.0, but I'd like to improve my old 4.7 code so that it is working on both OS.

 

I have a socket connection between my desktop pc and my BlackBerry storm (and bold). It is working fine on 4.7 although everything that the socket sends is mixed up when it arrives. That is not the actual problem, but if anyone has a hint for me - would be great!

I'm using wap2 for that.

On 5.0 it sends everything clearly (great) but my receive code is not working. It knows when data is arriving by checking the InputReaderStream's .ready(). I think this is working, it's always the right time. Then I'm trying to read it by .read(). It always blocks and waits although there IS something. But it doesn't start to read something until(!) the connection is closed by the other side (my desktop pc).

Can you give me a hint what I'm doing wrong?

That happens only on the device, not in the simulator!

 

That is what I tried:

 - using a DataInputStream (I were not able to find a difference)

 - using another .read() overload

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: another socket problem: socket is not receiving although it is ready

Does your ServerSide processing do a flush?

Developer
Posts: 47
Registered: ‎05-06-2009
My Device: Not Specified

Re: another socket problem: socket is not receiving although it is ready

Thank you for your reply.

 

I'm doing a flush but it looks that it makes no difference. But I'll have a closer look at that thing.

is it possible that it is depending on the flush although it works on 4.7?

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: another socket problem: socket is not receiving although it is ready

Can we see your BlackBerry code and the line you know it is blocking on.  I do mean 'know', if you could put some tracing in to prove this line, that would be desirable.  Sometimes blocks occur in places people do not expect. 

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: another socket problem: socket is not receiving although it is ready

[ Edited ]

Is you receive code running on a BlackBerry? If so, this is a known issue. One of the solutions which used to work (sometimes) in the past is to read byte-by-byte using the InputStream.read(void) method. If this doesn't help, then it might be a good idea for you to post your reading code here...

Developer
Posts: 47
Registered: ‎05-06-2009
My Device: Not Specified

Re: another socket problem: socket is not receiving although it is ready

[ Edited ]

Thank you again!

 

At first: I tried a lot of things with the serverside .flush(). I think the server works as intended but I'm not that sure.

But it is working on 4.7 (device + simulator) and on 5.0 in the simulator, too.

 

Here's a shortened "trace" to the line where I think it blocks:

 

 

connection = (StreamConnection)Connector.open("socket://" + _ip + ":80;deviceside=true;ConnectionUID=WAP2 trans", Connector.READ_WRITE);
_in = new InputStreamReader(connection.openInputStream());

// some thread things...

if (_in.ready())
{
try
{
iChar = _in.read(); // doesn't work, although data is available
}
catch(Exception e)
{}
}

 

Can you help me?

 

 

//Edit: Thank you too klyubin. Yes it runs on a blackberry. I expect that reading byte by byte will not be possible because it simply doesn't read anything but I'll give it a try Smiley Happy

 

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: another socket problem: socket is not receiving although it is ready

P.S. Get rid of InputStreamReader and use the InputStream.read(void) method directly to read one byte at a time.

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: another socket problem: socket is not receiving although it is ready

P.P.S. Also, get rid of InputStreamReader.ready() check -- let the InputStream.read(void) block if necessary.

Developer
Posts: 47
Registered: ‎05-06-2009
My Device: Not Specified

Re: another socket problem: socket is not receiving although it is ready

Using InputStream.read() did the trick!

_in.ready is replaced by _in.available > 0 which works fine.

 

Thank you both very much!

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: another socket problem: socket is not receiving although it is ready

Be careful with InputStream.available() -- it's not required or guaranteed to return non-zero values when there are data that can be read without blocking.