TCP receive : how to get all message bytes?


I am trying to receive data with a tcp connection. The thing is that I do not always get to read all data that were written to the socket and the number of bytes that I read seems to be random.

As an example, the client sent 49kB, which I confirmed that were received from Xcode network tools in the debug area, but programmatically I received only 17kB, 2kB…

The strange thing is that when I put a breakpoint and run it step by step, I always read all bytes that were sent. I thought that this might be a thread problem, which is why I put the ScopedLock before looping to take all the data. But still the reaction is the same with or without it…

Variables declaration in .h and allocation in setup function :

/* *variables declaration* */
int receiveSize = 177400, bigSize = 177400;        
ofBuffer tcpbuffer;

/* *setup function* */

And the code inside the update function :

for (int client = 0; client < TCPserver.getLastID(); client++) {
    int totalBytes = 0, result = 0;
    if( TCPserver.isClientConnected(client) ) {

        //  lock until message in socket is completly read!
            ofMutex::ScopedLock lock(mutex);                // ofScopedLock lock(mutex);
            while (result >= 0) {
                result = TCPserver.receiveRawBytes(client, &tcpbuffer.getBinaryBuffer()[totalBytes], receiveSize*sizeof(char));
                totalBytes += result;
            // last time result returns -1
            totalBytes -= result;
        char choice = tcpbuffer.getBinaryBuffer()[0];

      *... do something with the buffer.....*


Many thanks in advance!

I changed the size of the tcpbuffer to (a big number like) 10240000 and everything worked properly!
Isn’t it a bit strange though, as it was set at 177400 and I was supposed to receive around 49000?