The socket inputstream functions read(byte b) and read(byte b, int offset, int len) functions should return after reading few bytes whenever they are available.
***Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read, possibly zero. The number of bytes actually read is returned as an integer. ***
But when I run this app on Nokia 6600, the app gets hang. Basically its get blocked on read() as am passing a big buffer. Then I realized it doesn't return until len bytes are read or buffer if full. So if I don't know the len of message to be read, I have to read byte by byte which consumes too much processor time.
Anybody realized/faced this problem? I couldn't run it on any other device as my App uses MIDP2.0 APIs.
I have Firmware 4.09.1 on my 6600. How I can get Firmware 3.42.1 on it?
The Known issues in Nokia 6600 document says that read(byte b) and read(byte b, int offset, int len) doesn't guarantee that specified size of data will be read. I am fine with it, but my problem is it doesn't return until it reads len size or until buffer if full.
I am reading from inputstream in loop only to make sure I read the complete buffer. But since my buffer size is bigger than actual data sent from other side, the read never returns and keep waiting for buffer to be full.
This is what am doing -
//inputBuffer is big buffer (5000 bytes) to store socket read data
//buffer is 1500 bytes buffer
bytes = socketInputStream.read(buffer, 0, CHUNK_SIZE);
bytesRead += bytes;
inputBuffer.write(buffer, bytesRead, bytes);
if (bytes == -1 || GotEndOfHeader(buffer))
The data size to be read from socket is not known and end of packet is indetified by marker [similiar to end of header ("\r\n\r\n")marker for http] So I read in 1500 chunk size
Problem comes when other side sents only 200 bytes. The read in this case never returns and keep waiting till it read CHUNK_SIZE data, which server is never going to send. So it blocks forever.
As per documentation, the read should return with some bytes as soon as they are available on socket. It shud return either 200 bytes or anything less than 200. But it just keep waiting until CHNUK_SIZE ia available on socket.
THIS MEANS read() IS IMPLEMENTED AS readFully() ON NOKIA 66600 MIDP IMPLEMENTATION.
Just a thought: Maybe the proper way to send data is first by sending the length. then use a for loop to small chunks. If you will query the size of the device's socket's buffer ( maybe 64K? ) you will get much better preformance and other threads can work as well.
I know the best thing is to send size in first few bytes of data. But it works fine when your protocol has fixed size header. In my case the header size is not fixed and end of header is determined by a marker "\r\n\r\n". And this is the case for HTTP and SIP protocols. Mostly you will find end of header marker in text based protocols. The header though contains Content length, which specifies the size of body.
But reading header is giving pain as I have to read byte by byte and check if end of header is read. It would have been good if I can read it in one go (in a big buffer) and then parse the buffer to find end of header.
My question is Have anybody also realized that MIDP2.0 on Nokia 6600 has implemented socket read() as readFully()? Though the documentation says that it supports only read() and readFully() is not supported, but internally the read() behaves like readFully().