Problem using SocketConnection with Blackberry using MDS

I am currently writing an application on Blackberry to simply send and receive some raw data to another TCP based device on my network. I have the same problem in a Blackberry simulator with an MDS simulator and using a physical phone talking to my company's MDS server. Please note that this problem does not occur when using Wi-Fi directly, and not through MDS.

The problem is that the available () function in InputStream returns zero unless I call read () first. If I call first by reading (knowing that there is some data .. thanks wirehark), the data is returned, and a subsequent call to available () indicates what data remained that I did not read. The problem is that I will not always be guaranteed that the data will be there, and therefore I can block it. Does anyone know about this and is it a problem or something that is in design?

Does anyone know how to check if the read () method will block before calling them away from the available ones?

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 on the if condition for this to work.

+5
source share
2 answers

, , , , , , "ping" . , . . , , , (...) RIM InputStream, , . , , () 0? - , - , .

, , . , InputStream () (...). , , , , .

InputStream, , , . , , , . , .

, . .

0

InputStream.available() , " , ( ) ." , , , . ,

byte[] readFromStream(InputStream is) throws IOException
{
    byte[] data = new byte[4096];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);

    int count = is.read(data, 0, data.length);
    while (count != -1)
    {
        dos.write(data, 0, count);
        count = is.read(data, 0, data.length);
    }

    data = baos.toByteArray();

    return data;
}

readFromStream() [].

+3

All Articles