How to handle large amount of data

Hi all,

I have developed a client/server application and make use of socket to send/rcv data (data size: around 12KBytes), but I found that the input stream cannot fully read the data from server and the data I got an incompleted data. What happen with the socket and did anybody got the same experience as me and please kindly suggest some possible way to solve it. Thanks!!

Cheers,

oh_silly

[417 byte] By [oh_sillya] at [2007-11-27 9:37:45]
# 1
12k is not a large amount of data.InputStream.read() returns the number of bytes actually read. It may be less than you expected. In that case you have to read again, ...All this is specified in the Javadoc for InputStream.read().
ejpa at 2007-7-12 23:09:41 > top of Java-index,Core,Core APIs...
# 2
Hi ejp,Thanks for your information, but how do determine the end of reading from inputstream?Cheers,oh_silly
oh_sillya at 2007-7-12 23:09:41 > top of Java-index,Core,Core APIs...
# 3
that too is explained in the Javadoc...
jwentinga at 2007-7-12 23:09:41 > top of Java-index,Core,Core APIs...
# 4

Yup, u are right.

Actually it can be done by the following script:

Inputstream is = socket.getInputStream();

int got = 0;

while((got = is.read()) != -1){

System.out.println("......");

}

But will there be any case which makes the program loop inside while? My case is that the inputstream has received all necessary bytes from server but it can escape from the loop. Did my program have bug or the EOF flag was not issued by server? Please kindy advise.

Thanks for you guys attentions.

Cheers,

oh_silly

oh_sillya at 2007-7-12 23:09:41 > top of Java-index,Core,Core APIs...
# 5

Use Socket.setSoTimeout() to set a read timeout in milliseconds to avert this case. I would set the timout at at least a minute. If it triggers you will get a SocketTimeoutException.

Also you should write that loop like this:

byte[] buffer = new byte[8192];

int count;

while ((count = in.read(buffer)) > 0)

out.write(buffer, 0, count);

Reading into a buffer is many times as efficient as reading a byte at a time.

ejpa at 2007-7-12 23:09:41 > top of Java-index,Core,Core APIs...
# 6
thanks ejp,I can receive all the data from InputStream now, but I have to find out the problem of staying in in.read() even all bytes have been retrieved.Cheers,oh_silly
oh_sillya at 2007-7-12 23:09:41 > top of Java-index,Core,Core APIs...
# 7
Count them. When you've retrieved all the bytes you want to read, don't read any more bytes.If this is HTTP 1.1 or a similar protocol with persistent connections, the server won't close the socket at the end of one reponse, so you won't get the -1 that signals EOS.
ejpa at 2007-7-12 23:09:41 > top of Java-index,Core,Core APIs...
# 8
Hi ejb,But I don't know the exact volume of bytes to be received. How can I predict the amount of data?Cheers,oh_silly
oh_sillya at 2007-7-12 23:09:41 > top of Java-index,Core,Core APIs...
# 9
If the server isn't going to close the connection after sending the response, you have to know the size of the response. Either the server has to prefix a length word, or the response has to be in some self-describing format like XML.
ejpa at 2007-7-12 23:09:41 > top of Java-index,Core,Core APIs...