02-08-2010 06:47 AM - edited 02-08-2010 10:21 AM
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
Solved! Go to Solution.
02-08-2010 08:07 AM
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?
02-08-2010 08:21 AM
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.
02-08-2010 12:47 PM - edited 02-08-2010 12:48 PM
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...
02-08-2010 12:48 PM - edited 02-08-2010 12:51 PM
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...
iChar = _in.read(); // doesn't work, although data is available
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
02-08-2010 01:24 PM
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.