Results 1 to 2 of 2

Thread: QTcpSocket in QThread

  1. #1
    Join Date
    Oct 2009
    Posts
    5

    Default QTcpSocket in QThread

    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

    Qt Code:
    1. bool TestServerThread::receiveString(QTcpSocket& sock, QString& str)
    2. bool TestServerThread::receiveString(QTcpSocket* sock, QString& str)
    To copy to clipboard, switch view to plain text mode 

    Actual code:

    Qt Code:
    1. TestServerThread::TestServerThread(int socketDescriptor, QObject *parent) : QThread(parent), socketDescriptor(socketDescriptor)
    2. {
    3. }
    4.  
    5.  
    6. bool TestServerThread::receiveString(QTcpSocket& sock, QString& str)
    7. {
    8. if(sock.isValid())
    9. {
    10. if(!sock.waitForReadyRead(30))
    11. {
    12. qWarning() << "fail"; // fails here
    13. return false;
    14. }
    15. QByteArray buf = sock.readAll();
    16. str = buf;
    17. }
    18. }
    19.  
    20. void TestServerThread::run()
    21. {
    22. QTcpSocket sock;
    23. if (!sock.setSocketDescriptor(socketDescriptor)) {
    24. emit error(sock.error());
    25. return;
    26. }
    27.  
    28. bool ok = true;
    29. while(ok)
    30. {
    31. QString str;
    32. //if(ok) ok = receiveString(sock, str);
    33.  
    34. if(!sock.waitForReadyRead(30))
    35. {
    36. qWarning() << "false";
    37. }
    38. QByteArray buf = sock.readAll(); // same routine succeeds
    39. str = buf;
    40. qWarning() << str;
    41.  
    42. qWarning() << "Received: " << str;
    43. if(ok) ok = sendString(sock, "kaka");
    44. }
    45. sock.disconnectFromHost();
    46. sock.waitForDisconnected();
    47. }
    To copy to clipboard, switch view to plain text mode 

    When you subclass QTcpSocket - same error results
    sock.errorString() returns "Network operation timed out" in function receiveString(). In run() it succeeds
    Last edited by zverj; 12th October 2009 at 10:28. Reason: Solution found

  2. #2
    Join Date
    Oct 2009
    Posts
    5

    Default Re: QTcpSocket in QThread

    My stupid mistake of timeout too low.

Similar Threads

  1. QTcpSocket in QThread
    By Alex Snet in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2009, 20:02
  2. QThread and QTcpSocket
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 19th May 2008, 18:43
  3. QThread and QTcpSocket
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2008, 14:06
  4. Problem with QTcpSocket in QThread
    By Raistlin in forum Qt Programming
    Replies: 8
    Last Post: 6th October 2007, 13:23
  5. Problems with QThread and QTcpSocket
    By cookiem in forum Qt Programming
    Replies: 6
    Last Post: 2nd November 2006, 09:25

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.