By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 822 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

DataInputStream --- random incorrect parsing...

P: n/a
Hi,
I am working on a small java client and server program pair which at
client side packetizes data (such as jpeg image file) and appends some
header info for each packetized packet. At the server side, the
packetized data is reassembled back into the original image.

At client side I am using DataOutputStream/BufferedOutputStream:

DataOutputStream data_out = new DataOutputStream(
new BufferedOutputStream(client_socket.getOutputStream ()));
data_out.writeInt(seq_Num); // part
data_out.writeLong(time_stamp); // of
data_out.writeInt(image_size); // header
data_out.writeInt(length); // for each packetized data
data_out.write(data, 0, length); // Packetized Data
On the server side I read back packetized data back sent by client and
later reassemble it back to original image:

DataInputStream data_in = new DataInputStream(
new BufferedInputStream(connection_socket.getInputStre am()));

for ( ; ; ) {
seq_num = data_in.readInt();
time = data_in.readLong();
size = data_in.readInt();
dlength = data_in.readInt();
System.out.println(seq_num + ", " + time + ", " + size + ", "
+ dlength);

byte[] p_data = new byte[dlength];
rlen = data_in.read(p_data, 0, dlength);

Here's my problem
=================
When I read data back at the server side I get corrupted data when I
parse byte packetized data, that is data from DataInputStream data_in

Here's the output:

seq_num=1, time=1067733150359, size=65366, dlength=1000
Received Packet No. 1 from Node A . . .
seq_num=2, time=1067733150361, size=65366, dlength=1000
Received Packet No. 2 from Node A . . .
seq_num=3, time=1067733150363, size=65366, dlength=1000
Received Packet No. 3 from Node A . . .
seq_num=2134239293, time=5009899065144553973, size=-2132742293,
dlength=1480003007
Exception in thread "main" java.lang.OutOfMemoryError

Any ideas what causes this error? Any help would be greatly appreciated...

-John

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
John Thorner wrote:
On the server side I read back packetized data back sent by client and
later reassemble it back to original image:

DataInputStream data_in = new DataInputStream(
new BufferedInputStream(connection_socket.getInputStre am()));

for ( ; ; ) {
seq_num = data_in.readInt();
time = data_in.readLong();
size = data_in.readInt();
dlength = data_in.readInt();
System.out.println(seq_num + ", " + time + ", " + size + ", " +
dlength);

byte[] p_data = new byte[dlength];
rlen = data_in.read(p_data, 0, dlength);

Here's my problem
=================
When I read data back at the server side I get corrupted data when I
parse byte packetized data, that is data from DataInputStream data_in

Here's the output:

seq_num=1, time=1067733150359, size=65366, dlength=1000
Received Packet No. 1 from Node A . . .
seq_num=2, time=1067733150361, size=65366, dlength=1000
Received Packet No. 2 from Node A . . .
seq_num=3, time=1067733150363, size=65366, dlength=1000
Received Packet No. 3 from Node A . . .
seq_num=2134239293, time=5009899065144553973, size=-2132742293,
dlength=1480003007
Exception in thread "main" java.lang.OutOfMemoryError

Any ideas what causes this error? Any help would be greatly appreciated...

The DataInputStream.read(byte[], int, int) method is not guaranteed to
read all of the bytes requested. I see from your pseudo-code above you
are saving the return value; did you also check it to make sure all of
the requested bytes were read?

Ray

Jul 17 '05 #2

P: n/a
Raymond DeCampo wrote:
John Thorner wrote:
On the server side I read back packetized data back sent by client and
later reassemble it back to original image:

DataInputStream data_in = new DataInputStream(
new BufferedInputStream(connection_socket.getInputStre am()));

for ( ; ; ) {
seq_num = data_in.readInt();
time = data_in.readLong();
size = data_in.readInt();
dlength = data_in.readInt();
System.out.println(seq_num + ", " + time + ", " + size + ", "
+ dlength);

byte[] p_data = new byte[dlength];
rlen = data_in.read(p_data, 0, dlength);

Here's my problem
=================
When I read data back at the server side I get corrupted data when I
parse byte packetized data, that is data from DataInputStream data_in

Here's the output:

seq_num=1, time=1067733150359, size=65366, dlength=1000
Received Packet No. 1 from Node A . . .
seq_num=2, time=1067733150361, size=65366, dlength=1000
Received Packet No. 2 from Node A . . .
seq_num=3, time=1067733150363, size=65366, dlength=1000
Received Packet No. 3 from Node A . . .
seq_num=2134239293, time=5009899065144553973, size=-2132742293,
dlength=1480003007
Exception in thread "main" java.lang.OutOfMemoryError

Any ideas what causes this error? Any help would be greatly
appreciated...

The DataInputStream.read(byte[], int, int) method is not guaranteed to
read all of the bytes requested. I see from your pseudo-code above you
are saving the return value; did you also check it to make sure all of
the requested bytes were read?

Ray


Thanks for the tip. I should have used readFully() instead. It works!!!

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.