0

I am connecting my code to an existing server and i am able to get response from the server.

But the problem is the content of the response is too large to be handled in a simple way.

Right now below is the piece of code that i use it to retrieve data.

socket->waitForReadyRead(1000);
    array = socket->readAll();
    for(int i=0; i< array.size();i++){
        //qDebug()<< array[i];
        test += array[i];
    }

    qDebug()<< "cmd Part 2: ";
    socket->waitForReadyRead(1000);
    array = socket->readAll();
    for(int i=0; i< array.size();i++){
        //qDebug()<< array[i];
        test += array[i];
    }

I have problems with simplifying the above code with for loop.

I do not know how to implement the socket->waitForReadyRead inside my for loop. Can anyone please help me on this?

sankar
  • 347
  • 3
  • 4
  • 15
  • I think it's rather unclear what you are trying to do and what the problem is. Do you know how much data you are expecting to read? – dunc123 Aug 09 '13 at 10:06
  • Yes there are lots of them. Just imagine you are retreiving a table with 100 rows and 15 columns. I have to repeat the above code over and over again till i am able to retreive all the data. – sankar Aug 09 '13 at 10:11
  • @dunc123 not only that, i have to let the socket wait for everytime before i start reading it again. I am able to retreive all the data as i mentioned above but i want to simplify it by putting the above code in a a for loop. – sankar Aug 09 '13 at 10:13

1 Answers1

1

A basic solution would be:

while (socket->waitForReadyRead(1000))
{
    QByteArray array = socket->readAll();
    // do stuff
}

The loop will exit when the read times out or there is an error (e.g. the socket is closed).

dunc123
  • 2,595
  • 1
  • 11
  • 11