DataInputStream - random incorrect data reading...

J

John Thorner

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.getInputStream()));

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
 
J

John Thorner

Thanks, it works correctly now!!!


use

data_in.readFully(p_data, 0, dlength);



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.getInputStream()));

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top