Meaning of waitForReadyRead(-1)



  • Hello everybody,
    I'm a newby in QT and I don't have much OPP experience.
    I'm developing a multithreading server application using QTcpServer Class based on the example "Threaded Fortune Server Application".

    As in the example in the run function of the thread I create a QTcpSocket object, initialize the socket and then use the function waitForReadyRead in order to wainting data to be read.
    In the follow you can see I'm using the waitForReadyRead function in run function

    @
    void CThread::run()
    {
    QTcpSocket tcpSocket;

    if (!tcpSocket->setSocketDescriptor(socketDescriptor))   //Socket Initialization:
    {                                                        //If error
        emit error(tcpSocket->error());                      //emit error
        return;                                              //and go out
    }
    
    while (!close)
    {
         if (tcpSocket->waitForReadyRead(-1))    //Waiting data to be read
         {
               DataServer = tcpSocket->readAll();        //Read all data
            
                 //... Data decoding ...
                 //...and answering...
         }
    }
    

    }
    @

    I used the waitForReadyRead() funcition specifying in the argument -1 because I'd like to stop the thread waiting new data.

    When I tried my application I found out that the thread didn't stop in waitForReadyRead() waiting for new data, but It worked like a time-out occours. So that, an infinte loop in while (!close) { ... } goes on.

    I'd like to know if I understood the meaning of waitForReadyRead or I missed something.

    Can someone help me?

    Thanks in advance.


  • Moderators

    welcome to the forum

    waitForReadyRead will exit, when information is ready to read. So, if you are receiving a larger chunk of information it might exit several times.

    Also the description for this reimplementation does not refer to -1 "see here":http://doc.qt.nokia.com/4.7/qabstractsocket.html#waitForReadyRead
    it is only mentioned in "QIODevice":http://doc.qt.nokia.com/4.7/qiodevice.html#waitForReadyRead



  • The big question is: why do you do this? Why make your socket handling first asynchronic by putting it in a thread, and then use synchronic programming again within the thread? Why not use the asynchronic programming methods directly and get rid of both the thread and the waitFor* methods?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.