473,320 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

DataInputStream --- random incorrect parsing...

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
2 4787
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Alexandros Karypidis | last post by:
Hi all, I would like some advice regarding the following design issue: I would like to be able to exchange data in XDR format. I was thinking of writing a pair of I/O streams in the same...
6
by: Patrick | last post by:
Hello all! I am porting an application from C++ to Java and have run into a problem using the DataInputStream reader object. The file I am trying to read in is anywhere from 20 to 60 MB and has a...
25
by: JNY | last post by:
I am using random to generate random numbers, thus: int x,y; for (y = 0;y < 5;y++) { x = random(50); cout << x; }
24
by: Stavros Christoforou | last post by:
Hello everyone, I was wondering if someone could help me with an issue I have in C++. I want to select random points within the volume of a sphere. I know how to get random numbers using srand()...
18
by: Toby Newman | last post by:
I need to randomly choose one of four paths in my program. Using the tools I know, the best way I can think to do it is by doing something like the following: //==============================...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
3
by: Kza | last post by:
Hi, I am currently using xerces sax parser for c++, (I use DOM too, but I think SAX is more relevant here) for processing and displaying fairly large xml files. Usually I give xerces a filename,...
3
by: duffint | last post by:
Hi there, I have this script that I need some direction in; it's mangled my head a bit. I want to be able to stick links around six random words; these links are then popup ads. I saw kind...
20
by: Robbie Hatley | last post by:
I needed a quick program called "random" that gives a random positive integer from n1 to n2. For example, if I type "random 38, 43", I want the program to print a random member of the set {38,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.