I am currently writing an app on the Blackberry to do a simple send and receive of some raw data to another TCP based device on my network. I am having the same problem in the Blackberry simulator w/ an MDS simulator running and using a physical phone talking to my company's MDS server. Note this problem does not happen when using wifi directly and not via MDS.
The problem is that the available() function on the InputStream returns zero unless I call read() first. If I call read first (knowing there is some data available .. thank you wireshark) the data comes back, and the subsequent call to available() indicates what data is left that I did not read. The problem is that I am not always going to be guaranteed that data will be there and so I could block. Is anyone aware of this, and is this a problem or something that is by design?
Is anyone aware of a way to test if the read() method(s) will block before calling them aside from available?
Here is basically what I am doing:
SocketConnection s = (SocketConnection)Connector.open("socket://1.2.3.4:port;deviceside=false", Connector.READ_WRITE); OutputStream o = ((StreamConnection)s).openOutputStream(); InputStream i = ((StreamConnection)s).openInputStream(); o.write("hello"); Thread.sleep(sometime); if (i.available() > 0) { byte[] data = new data[10]; int bytesRead = i.read(data); System.out.println("Read [" + new String(data) + "] (bytes = " + bytesRead + ")"); }
I have to comment out the if conditional for this to work.