Results 1 to 7 of 7

Thread: Problems with QThread and QTcpSocket

  1. #1
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    7
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Problems with QThread and QTcpSocket

    When socket writes data i get the "QObject: Cannot create children for a parent that is in a different thread." error, but the data is transferred anyway, because client gets it. QTcpSocket has no parent. Could someone help me in solving this? (Qt 4.2)

    Here is the run():
    Qt Code:
    1. void XRServerThread::run()
    2. {
    3. m_socket = new QTcpSocket;
    4.  
    5.  
    6. //end if error occured
    7. if(! m_socket->setSocketDescriptor(m_socket_descriptor))
    8. {
    9. //-32300 - xr transport error
    10. emit error(XR_TRANSPORT_ERROR, QString("Socket error: %1")
    11. .arg(m_socket->error()));
    12. return;
    13. }
    14.  
    15. connect(m_socket, SIGNAL(readyRead()), this, SLOT(readFromSocket()));
    16. // connect(m_socket, SIGNAL(disconnected()), m_socket,
    17. //SLOT(deleteLater()));
    18. //exit the thread after disconnection
    19. connect(m_socket, SIGNAL(disconnected()), this, SLOT(quit()));
    20.  
    21. //start event loop
    22. exec();
    23. }
    To copy to clipboard, switch view to plain text mode 
    .
    .
    .

    Here is the place where error occurs (m_socket->write()):
    Qt Code:
    1. void XRServerThread::sendHttpResponse(int status_code, const QString &reason,
    2. QHttpHeader & headers, QString resp)
    3. {
    4. //This is the body of the response:
    5. QByteArray output;
    6. output.append(resp);
    7.  
    8. headers.setContentLength(output.size());
    9.  
    10. //This is the headers:
    11. QByteArray head_out;
    12. head_out.append(QString("HTTP/1.1 %1 %2\r\n")
    13. .arg(QString::number(status_code)).arg(reason));
    14. QStringList keys = headers.keys();
    15.  
    16. //creating string representation of headerss
    17. foreach(QString key, keys)
    18. {
    19. head_out.append(QString("%1: %2\r\n").arg(key).arg(headers.value(key)));
    20. }
    21.  
    22. head_out.append("\r\n");
    23.  
    24. //Write the headers out:
    25. m_socket->write(head_out);
    26.  
    27. //Write the body out:
    28. m_socket->write(output);
    29.  
    30. //Close the connection we will not write anything else
    31. m_socket->disconnectFromHost();
    32. m_socket->waitForDisconnected();
    33. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problems with QThread and QTcpSocket

    How does sendHttpResponse() get called? Most likely XRServerThread::sendHttpResponse() is executed in a different thread than what is being executed in XRServerThread::run(). An easy way to assure this is is to check QThread::currentThread() in both functions.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    cookiem (31st October 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    7
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QThread and QTcpSocket

    Thanks! You're right it's called from different thread, but how to ensure that write() will be called from the same thread? Use QThreadStorage, or i don't get something?

  5. #4
    Join Date
    Nov 2006
    Posts
    5
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QThread and QTcpSocket

    How about sending a signal to the thread, so that sendHttpResponse is invoked via the event queue.

  6. #5
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QThread and QTcpSocket

    Quote Originally Posted by cookiem View Post
    Thanks! You're right it's called from different thread, but how to ensure that write() will be called from the same thread? Use QThreadStorage, or i don't get something?
    I'm not sure but putting your code in another function which will be called from QThread::run() might help...
    Current Qt projects : QCodeEdit, RotiDeCode

  7. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problems with QThread and QTcpSocket

    I never got an answer to my question, "How does sendHttpResponse() get called?" ..but anyway, I assume it gets called in consequence of the socket emitting a certain signal. Slots in a QThread object get executed in the thread where it lives in (that is the thread where it was created in), not in the thread which is being executed in QThread::run(). Check this thread.

    In my opinion the easiest solution would be to subclass QTcpSocket and write the functionality (which is currently in XRServerThread slots) into it. Then those slots would automatically get executed in correct thread.
    J-P Nurmi

  8. The following user says thank you to jpn for this useful post:

    cookiem (2nd November 2006)

  9. #7
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    7
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with QThread and QTcpSocket

    Quote Originally Posted by jpn View Post
    I never got an answer to my question, "How does sendHttpResponse() get called?" ..but anyway, I assume it gets called in consequence of the socket emitting a certain signal. Slots in a QThread object get executed in the thread where it lives in (that is the thread where it was created in), not in the thread which is being executed in QThread::run(). Check this thread.

    In my opinion the easiest solution would be to subclass QTcpSocket and write the functionality (which is currently in XRServerThread slots) into it. Then those slots would automatically get executed in correct thread.
    Oh, i am so sorry, i missed that. You are right it's called from a slot. Thanks for the link and everything else My question was answered! Thanks again!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.