02-08-2010 06:47 AM - edited 02-08-2010 10:21 AM
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
Solved! Go to Solution.
02-08-2010 07:03 AM
Does your ServerSide processing do a flush?
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...
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 ![]()
02-08-2010 12:49 PM
P.S. Get rid of InputStreamReader and use the InputStream.read(void) method directly to read one byte at a time.
02-08-2010 12:52 PM
P.P.S. Also, get rid of InputStreamReader.ready() check -- let the InputStream.read(void) block if necessary.
02-08-2010 01:21 PM
Using InputStream.read() did the trick!
_in.ready is replaced by _in.available > 0 which works fine.
Thank you both very much!
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.