0

I'm sending multiple byte arrays with size <500 over TCP. However, on the client side, I sometimes receive arrays with size >2000. Why is this happening?

Below is my code for TCPClient:

       byte[] receiveData = new byte[64000];            
       DataInputStream input = new DataInputStream(socket.getInputStream());
       int size = input.read(receiveData);
       byte[] receiveDataNew = new byte[size];
       System.arraycopy(receiveData,0,receiveDataNew,0,size);
       System.out.println("length of receiveData is " + size);
       GenericRecord result = AvroByteReader.readAvroBytes(receiveDataNew);
       return result;

Any help is appreciated! Thank you!

4

3 回答 3

1

TCP does not have any concept of message boundaries, since it is a stream protocol. It can (and will) merge multiple sends into a single receive, and even the opposite - split a send into multiple smaller receives.

Your application must be prepared for this.

于 2012-07-30T23:30:53.670 回答
1

TCP is not a record oriented protocol. It is stream oriented. That means that packets can be recombined before receiving on the other side. Likely, this means you have received 3 or 4 sets of data arrays, and are processing them all at once.

If you wish to use it as a record-oriented or framed protocol, you'll have to add that framing yourself. You could prepend a size to the data you send, and only read that much data at a time.

If your records are always the same length, you can do a fixed-length read. I don't know Java's libraries, but you should be able to provide a length to read().

于 2012-07-30T23:31:14.913 回答
1

Basically what you are seeing is multiple arrays combined back to back being received at the same time. Send the array length before the array and read that many bytes.

于 2012-07-30T23:33:17.810 回答