[Interest] ThreadedFortune example extending to send message forever

Nilesh Kokane nilesh.kokane05 at gmail.com
Sat Sep 3 18:13:51 CEST 2016


Hello,

I extended Threaded fortune to send message in infinite loop as below,
but then it only send once and I get the socket error. I've not
changed the fortune client but I only press Get fortune once and
expect the message to be sent continuously which isn't a case.

Is it really needed to abort() the tcpSocket from the client side and
connectToHost per message? Doing so I get the incomingConnection and
removing the forever loop of the below function it works as expected?



void FortuneThread::run()
{

   forever
    {
        QTcpSocket tcpSocket;
        QString temp_text =  text.at(qrand() % text.size());
        qDebug() << "text from thread" << temp_text;
        if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
            emit error(tcpSocket.error());
            qDebug() <<  "socket error";
            return;
        }
        QByteArray block;
        QDataStream out(&block, QIODevice::WriteOnly);
        out.setVersion(QDataStream::Qt_4_0);
        out << temp_text;
        //! [3] //! [4]

        tcpSocket.write(block);
        tcpSocket.disconnectFromHost();
        tcpSocket.waitForDisconnected();

    }
 }
}


Any clue?

-- 
          Nilesh Kokane



More information about the Interest mailing list