Buffered reading seems to corrupt data stream



I am trying to repeatedly send a byte array from a server to a client where both the writing and the reading is done in a buffered manner with the following code on the server:

DataOutputStream dos = new DataOutputStream(os);
dos.writeInt(bytes.length);
dos.write(bytes);
dos.flush();

and this on the client:

System.out.println("Reading size...");
final DataInputStream dis = new DataInputStream(new BufferedInputStream(is));
final int size = dis.readInt();
final byte[] bytes = new byte[size];
System.out.println("Reading " + size + " bytes...");
dis.readFully(bytes);

The problem is that for the second or third read, the size variable is coming back as garbage (extremely high or even negative value) and the reading of the byte array blocks as it waits for a large amount of data to be received or crashes with the invalid negative size even though the previous size value and the previous bytes themselves are correct. However, if I remove the BufferedInputStream wrapping of stream, it works perfectly.

Why would that be?

--
And loving it,

-Qu0ll (Rare, not extinct)
_________________________________________________
Qu0llSixFour@xxxxxxxxx
[Replace the "SixFour" with numbers to email me]

.



Relevant Pages

  • Re: client-server issue
    ... intelligence in the server - if it is possible for the server to ... identify a client (e.g. client IP address/port, ... global variables in the server indicating which IP/port has control, ... Need further reading... ...
    (comp.os.linux.networking)
  • Re: Block a receive call
    ... If no data is available for reading, the Receive method will block until ... Sockets" after you intalled the 101 samples.And I am sure you will find out ... The server receives the information and sends an ACK to the client, ...
    (microsoft.public.dotnet.framework)
  • Blocking client and NIO server (Write back to the client issue)
    ... I have to develop a NIO server which can handle hundreds J2ME clients. ... The clients has no NIO capabilities and use blocking reading/writing ... I have no trouble to send msg from the client to the server ... On the client side, for reading data from the server, I did that: ...
    (comp.lang.java.programmer)
  • Re: Blocking client and NIO server (Write back to the client issue)
    ... I have to develop a NIO server which can handle hundreds J2ME clients. ... The clients has no NIO capabilities and use blocking reading/writing ... I have no trouble to send msg from the client to the server ... On the client side, for reading data from the server, I did that: ...
    (comp.lang.java.programmer)
  • Re: Buffered reading seems to corrupt data stream
    ... where both the writing and the reading is done in a buffered manner ... output stream being used in to the above code. ... between client and server is established I repeatedly send the fixed ... Then make sure that there is also only one BufferedInputStream on is and one BufferedOutputStream on os. ...
    (comp.lang.java.programmer)