views:

1285

answers:

2

I have simple server connection thread. When you call function receiveString, it fails. However when you execute same code in run(), it succeeds. What is needed for function receiveString to work?

I've tried both

bool TestServerThread::receiveString(QTcpSocket& sock, QString& str)
bool TestServerThread::receiveString(QTcpSocket* sock, QString& str)

Actual code:

TestServerThread::TestServerThread(int socketDescriptor, QObject *parent) : QThread(parent), socketDescriptor(socketDescriptor)
{
}


bool TestServerThread::receiveString(QTcpSocket& sock, QString& str)
{
    if(sock.isValid())
    {
        if(!sock.waitForReadyRead(30))
        {
            qWarning() << "fail"; // fails here
            return false;
        }
        QByteArray buf = sock.readAll();
        str = buf;
    }
}

void TestServerThread::run()
{
    QTcpSocket sock;
    if (!sock.setSocketDescriptor(socketDescriptor)) {
        emit error(sock.error());
        return;
    }

    bool ok = true;
    while(ok)
    {
        QString str;
        //if(ok) ok = receiveString(sock, str);

        if(!sock.waitForReadyRead(30))
        {
            qWarning() << "false";
        }
        QByteArray buf = sock.readAll(); // same routine succeeds
        str = buf;
        qWarning() << str;

        qWarning() << "Received: " << str;
        if(ok) ok = sendString(sock, "kaka");
    }
    sock.disconnectFromHost();
    sock.waitForDisconnected();
}
+1  A: 

What is the return value or QTcpSocket::errorString()?

//...
if(!sock.waitForReadyRead(30))
{
    qWarning() << "fails " << sock.errorString(); // fails here
    emit error(sock.error());
    return false;
}
//...

Note that in the run, you are always reading the data on the socket, even if QTcpSocket::waitForReadyRead returned false.
Are you sure you don't get the same error in the run as in receiveString, but the read succeeds because you ignore this error?

Is run still succeeding if you use this code:

bool ok = true;
while(ok)
{
    QString str;
    //if(ok) ok = receiveString(sock, str);

    if(sock.waitForReadyRead(30))
    {
        QByteArray buf = sock.readAll(); // same routine succeeds
        str = buf;
        qWarning() << str;
        qWarning() << "Received: " << str;
        if(ok) 
        {
            ok = sendString(sock, "kaka");
        }
    }
    else
    {
         qWarning() << "fails " << sock.errorString();
         ok = false;
    }
}
TimW
sock.errorString() returns "Network operation timed out"That of course in function receiveString(). In run() it succeeds.
Pavels
+1  A: 

either somewhere of your code ruined the process stack, or your timeout value if not enough.

EffoStaff Effo
My stupid mistake. I've assumed wait time is in seconds, but it is time in miliseconds. Thank you for your answer. Btw, how I can ruin process stack?
Pavels
But your "wait time out" is the same for on `run` and `receiveString`. So `run` should fail for the same reason!
TimW
30 msec is really small time frame.
Pavels
a better way to understand that is: deeper stack if in receiveString.
EffoStaff Effo
EffoStaff Effo
Just the opposite! Run is faster than receiveString, so run should fail and receiveString should succeed.
TimW
TimW, maybe my wrong comment misleaded you which i deleted already. sorry about it.
EffoStaff Effo