DataInputStream - random incorrect data reading...

Discussion in 'Java' started by John Thorner, Nov 2, 2003.

  1. John Thorner

    John Thorner Guest

    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
     
    John Thorner, Nov 2, 2003
    #1
    1. Advertising

  2. John Thorner

    ak Guest

    use

    data_in.readFully(p_data, 0, dlength);



    "John Thorner" <> schrieb im Newsbeitrag
    news:5mYob.135805$k74.86885@lakeread05...
    > 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
    >
     
    ak, Nov 2, 2003
    #2
    1. Advertising

  3. John Thorner

    John Thorner Guest

    Thanks, it works correctly now!!!



    ak wrote:
    > use
    >
    > data_in.readFully(p_data, 0, dlength);
    >
    >
    >
    > "John Thorner" <> schrieb im Newsbeitrag
    > news:5mYob.135805$k74.86885@lakeread05...
    >
    >>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
    >>

    >
    >
    >
     
    John Thorner, Nov 2, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Berg
    Replies:
    1
    Views:
    7,311
    Esmond Pitt
    Nov 26, 2003
  2. Patrick
    Replies:
    6
    Views:
    921
    Roedy Green
    Jul 13, 2004
  3. James Vanns
    Replies:
    18
    Views:
    1,760
    Esmond Pitt
    Apr 8, 2005
  4. globalrev
    Replies:
    4
    Views:
    818
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,331
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page