QLocalSocket waitForReadyRead causes unexpected warning



  • I'm using QLocalSocket to use bidirectional communication with a QLocalServer for IPC. The scheme is simple, the client thread sends a message or request and waits for the server to return result data.

    The client writes to the server successfully with the following code:

    @if (!_socket->isOpen())
    {
    qDebug("(!) Connection closed");
    return ERR_E_CLIENT_CONNECTION_CLOSED;
    }

    QByteArray data;
    QDataStream out(&data, QIODevice::ReadWrite);
    out.setVersion(QDataStream::Qt_5_2);
    
    cmd.toDataStream(out);
    
    // write blocksize at first field
    
    out.device()->seek(0);
    out << data.size() - sizeof(BLOCKSIZE_T);
    _socket->write(data);
    _socket->flush();
    _socket->waitForBytesWritten();
    

    )@

    I wait for the server to write a response with

    // wait for response

    @ if (!_socket->waitForReadyRead(5000))
    {
    qDebug("(!) Waiting for command result: Operation timeout.");
    return _E_TIMEOUT;
    }
    else
    {
    QByteArray data = _socket->readAll();

        qDebug("Received response from server, data size=%d bytes.", data.length());
    
        if (data.length() > 0)
        {
            QDataStream in(&data, QIODevice::ReadOnly);
            resp.fromDataStream(in);
        }
        else
        {
            return _E_NO_DATA;
        }
    }
    

    @

    I'm getting the expected response from the server, but in the line:

    @
    if (!_socket->waitForReadyRead(5000))
    @

    i get the following warning dumped at the command line:

    QObject::startTimer: Timers can only be used with threads started with QThread

    Note that the client code is at present, a test suite, that does not create any thread, so assume this is a single-threaded, no-event loop program. I expect the waitForReadyRead method to not care about event loops.

    Should I issue the R/W operations for the socket in a new thread ?

    Thank you very much.



  • Is it waiting for 5000 msecs ? If yes don't worry about this warning. Looks like you are doing everything in main thread. This wait may cause you main thread to hang and not show or delay UI. So it is not a good practice to give sleep/wait in main thread. For your program if this is not causing issue don't worry.


Log in to reply
 

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