I have some C++ code that is writing to a socket that is being read from by a Java program.
The C++ program uses boost::async_write to transfer the bytes, and they are then read by the Java program. The first 4 bytes of the async_write call correspond to the size of the underlying data segment. Because the size is explicitly determined by this header, the Java code can fill a byte array that is the size of the actual data using a BufferedInputStream’s “read” method.
Although this seems to work fine almost every time, sometimes the byte array read by the InputStream contains zeroed out data for the end of its buffer. About 1 out of every 50000 messages is “corrupted” in this way. The place in the buffer where the zeroed out data starts varies. Although I can catch this and throw out the message I am wondering if this is expected performance? I have tried this where both programs are communicating through a socket on localhost and still have the problem.
I do not believe the bytes being written in the C++ program are going out of scope, because the are temporarily stored on a queue until the “handle_write” returns (from the async_write)
The C++ program only has a single thread executing the io_service.
The Java program is executing the reading in a single thread as well.
I did not detect any memory leaks in either program.
Most likely, this bug is on the reading side. Are you sure you actually read the entire message?