Problem with StreamingSocket::read. Not all data is received


I’m fairly new to sockets so I must be missing something fundamental.

I am trying to read a data stream from a StreamingSocket. The bytes are coming from a server running in Java. For this development and testing , I’m running the server on localhost. On the server side, I am writing the data in batches since it exceeds the maximum for a socket. So, for testing, I do maybe several hundred writes.


for(...) // Around 200 times.
        // Some processing.
        MySocket.getOutputStream().write(Buffer, 0, Buffer.length);

On the C++ side (JUCE), I have a loop ready where I try to read the data:

    int AllClear = 1;
    while (1 == AllClear)
        memset(Buffer, 0, BuffSize);
        int iBytesRead = m_poConnectedSocket->read(Buffer, BuffSize, false);
        if (iBytesRead > 0)
             //  Store the current buffer for later processing.          
            AllClear = m_poConnectedSocket->waitUntilReady(true, 10);

I have tried many versions of this loop and many other attempts at solutions. For debug purposes, I’ve even tried an infinite loop.The problem is that sometimes I get all the data. Sometimes nothing. Sometimes part of it. It’s completely random. Any suggestions are appreciated.