DataInputStream --- random incorrect parsing...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Thorner

    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 DataOutputStrea m/BufferedOutputS tream:

    DataOutputStrea m data_out = new DataOutputStrea m(
    new BufferedOutputS tream(client_so cket.getOutputS tream()));
    data_out.writeI nt(seq_Num); // part
    data_out.writeL ong(time_stamp) ; // of
    data_out.writeI nt(image_size); // header
    data_out.writeI nt(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 BufferedInputSt ream(connection _socket.getInpu tStream()));

    for ( ; ; ) {
    seq_num = data_in.readInt ();
    time = data_in.readLon g();
    size = data_in.readInt ();
    dlength = data_in.readInt ();
    System.out.prin tln(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=1067733150 359, size=65366, dlength=1000
    Received Packet No. 1 from Node A . . .
    seq_num=2, time=1067733150 361, size=65366, dlength=1000
    Received Packet No. 2 from Node A . . .
    seq_num=3, time=1067733150 363, size=65366, dlength=1000
    Received Packet No. 3 from Node A . . .
    seq_num=2134239 293, time=5009899065 144553973, size=-2132742293,
    dlength=1480003 007
    Exception in thread "main" java.lang.OutOf MemoryError

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

    -John

  • Raymond DeCampo

    #2
    Re: DataInputStream --- random incorrect parsing...

    John Thorner wrote:[color=blue]
    > 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 BufferedInputSt ream(connection _socket.getInpu tStream()));
    >
    > for ( ; ; ) {
    > seq_num = data_in.readInt ();
    > time = data_in.readLon g();
    > size = data_in.readInt ();
    > dlength = data_in.readInt ();
    > System.out.prin tln(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=1067733150 359, size=65366, dlength=1000
    > Received Packet No. 1 from Node A . . .
    > seq_num=2, time=1067733150 361, size=65366, dlength=1000
    > Received Packet No. 2 from Node A . . .
    > seq_num=3, time=1067733150 363, size=65366, dlength=1000
    > Received Packet No. 3 from Node A . . .
    > seq_num=2134239 293, time=5009899065 144553973, size=-2132742293,
    > dlength=1480003 007
    > Exception in thread "main" java.lang.OutOf MemoryError
    >
    > Any ideas what causes this error? Any help would be greatly appreciated...
    >[/color]
    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

    Comment

    • John Thorner

      #3
      Re: DataInputStream --- random incorrect parsing...

      Raymond DeCampo wrote:[color=blue]
      > John Thorner wrote:
      >[color=green]
      >> 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 BufferedInputSt ream(connection _socket.getInpu tStream()));
      >>
      >> for ( ; ; ) {
      >> seq_num = data_in.readInt ();
      >> time = data_in.readLon g();
      >> size = data_in.readInt ();
      >> dlength = data_in.readInt ();
      >> System.out.prin tln(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=1067733150 359, size=65366, dlength=1000
      >> Received Packet No. 1 from Node A . . .
      >> seq_num=2, time=1067733150 361, size=65366, dlength=1000
      >> Received Packet No. 2 from Node A . . .
      >> seq_num=3, time=1067733150 363, size=65366, dlength=1000
      >> Received Packet No. 3 from Node A . . .
      >> seq_num=2134239 293, time=5009899065 144553973, size=-2132742293,
      >> dlength=1480003 007
      >> Exception in thread "main" java.lang.OutOf MemoryError
      >>
      >> Any ideas what causes this error? Any help would be greatly
      >> appreciated...
      >>[/color]
      > 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
      >[/color]

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

      Comment

      Working...