I want to receive data from server again and again in a while loop , but it seems some strange things happen. The server has sent the data through a socket, and the data format is "data length + data". And if the client connect to the server by a socket connection, the server will send the data to client.
the receiving codes are :
byte buf = new byte;
SocketConnection sock = (SocketConnection)Connector.open(server.toString(),Connector.READ);
DataInputStream inStream = sock.openDataInputStream();
count = inStream.read( buf,0, buf.length);
if ( (count == 4) && (buf != null) )
dataLength = bytes2int(buf); //analyze the package head
imageData = new byte[dataLength];
count = inStream.read(imageData,0,imageData.length);
//process the imageData
And during the testing, I find that the program runs well at the beginning, but the dataLength will occur error sometimes, it's a big value and this makes the failture of the "new byte[dataLength]". From the log file of the server, I know that the datalength sent by server is OK.
So does anyone have any ideas?Thank you very much!