Results 1 to 9 of 9

Thread: Problem with QTcpSocket in QThread

  1. #1
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with QTcpSocket in QThread

    When I have a QThread which is responsible for handling a connection for a client application, I seem to have a strange problem. When I allocate a QTcpSocket in the constructor of the QThread, I do not succeed to open a connection afterwards in run(). When I do the allocation in run(), no problems arises. Any idea what might cause this ?

    Header file :

    Qt Code:
    1. #ifndef CONNECTIONTHREAD_H
    2. #define CONNECTIONTHREAD_H
    3.  
    4. #include <QThread>
    5. #include <QString>
    6.  
    7. class QTcpSocket;
    8.  
    9. class ConnectionThread : public QThread
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. ConnectionThread(QObject *parent);
    15. ~ConnectionThread();
    16.  
    17. bool isConnected() const;
    18.  
    19. signals:
    20. void connectionEstablished();
    21.  
    22. public slots:
    23. //void setHostInfo(QString& hostName, int portNumber);
    24.  
    25. private:
    26. void run();
    27.  
    28. QTcpSocket* tcpSocket_;
    29. bool connected_;
    30. QString hostName_;
    31. int portNumber_;
    32.  
    33. };
    34.  
    35. #endif // CONNECTIONTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    This works :

    Qt Code:
    1. #include "connectionthread.h"
    2. #include <QTcpSocket>
    3.  
    4. ConnectionThread::ConnectionThread(QObject *parent)
    5. : QThread(parent)
    6. {
    7. connected_ = false;
    8.  
    9. hostName_ = "localhost";
    10. portNumber_ = 4974;
    11.  
    12. connect(this, SIGNAL(connectionEstablished()), parent, SLOT(connectionEstablished()));
    13. }
    14.  
    15. ConnectionThread::~ConnectionThread()
    16. {
    17. }
    18.  
    19. bool ConnectionThread::isConnected() const
    20. {
    21. return connected_;
    22. }
    23.  
    24. void ConnectionThread::run()
    25. {
    26. tcpSocket_ = new QTcpSocket(this);
    27.  
    28. tcpSocket_->abort();
    29. tcpSocket_->connectToHost(hostName_, portNumber_);
    30.  
    31. if (tcpSocket_->waitForConnected(1000))
    32. emit connectionEstablished();
    33.  
    34. exec();
    35. }
    To copy to clipboard, switch view to plain text mode 


    This does not :

    Qt Code:
    1. #include "connectionthread.h"
    2. #include <QTcpSocket>
    3.  
    4. ConnectionThread::ConnectionThread(QObject *parent)
    5. : QThread(parent)
    6. {
    7. connected_ = false;
    8.  
    9. tcpSocket_ = new QTcpSocket(this);
    10.  
    11. hostName_ = "localhost";
    12. portNumber_ = 4974;
    13.  
    14. connect(this, SIGNAL(connectionEstablished()), parent, SLOT(connectionEstablished()));
    15. }
    16.  
    17. ConnectionThread::~ConnectionThread()
    18. {
    19. }
    20.  
    21. bool ConnectionThread::isConnected() const
    22. {
    23. return connected_;
    24. }
    25.  
    26. void ConnectionThread::run()
    27. {
    28. tcpSocket_->abort();
    29. tcpSocket_->connectToHost(hostName_, portNumber_);
    30.  
    31. if (tcpSocket_->waitForConnected(1000))
    32. emit connectionEstablished();
    33.  
    34. exec();
    35. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QTcpSocket in QThread

    I think the socket is created in the parent thread's context.
    Try calling connectionThread->moveToThread(connectionThread) from wherever you start the connection thread, but after it has started.

  3. #3
    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: Problem with QTcpSocket in QThread

    Are you sure you need a separate thread?

    http://www.qtcentre.org/forum/f-qt-p...cket-4242.html
    J-P Nurmi

  4. #4
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QTcpSocket in QThread

    Quote Originally Posted by jpn View Post
    Are you sure you need a separate thread?

    http://www.qtcentre.org/forum/f-qt-p...cket-4242.html
    It might be a similar title, but I did not see the link at first. I think more trivial questions have been asked more than one time in the past

  5. #5
    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: Problem with QTcpSocket in QThread

    If you would have bothered reading the thread, you would know that
    • QThread constructor and QThread::run() are executed in different threads
    • QThread object itself lives in the thread where it was created
    • You shouldn't pass "this" as parent in QThread::run() because "this" lives in another thread

    J-P Nurmi

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

    Raistlin (6th October 2007)

  7. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QTcpSocket in QThread

    Kids these days... They don't read anything, just want to give them the code.

  8. #7
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QTcpSocket in QThread

    I did not find the link first, so I was not aware of this. I understood the issue after reading the thread, and did not ask any additional things. So what makes you conclude I do not read the info which is given to me?

    Just for the record, I am certainly not a kid, and spent a lot of time resolving other issues in Qt without asking questions. I never asked for any code, I just raised a question about an issue I did not understand.

  9. #8
    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: Problem with QTcpSocket in QThread

    Looks like it was me who misunderstood your answer. I thought you meant that it was similar but something more trivial that was solved in the linked thread. My apologies for that. No hard feelings.
    J-P Nurmi

  10. #9
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QTcpSocket in QThread

    Indeed a misunderstanding, I just meant people ask the same question twice in this forum sometimes, although the issue is easier than this problem.

    No hard feelings back .

  11. The following user says thank you to Raistlin for this useful post:

    jpn (6th October 2007)

Similar Threads

  1. Strange QTcpSocket behavior (debugged with Ethereal)
    By mdecandia in forum Qt Programming
    Replies: 23
    Last Post: 28th May 2007, 20:44
  2. QTcpSocket speed problem
    By benelgiac in forum Qt Programming
    Replies: 4
    Last Post: 1st May 2007, 13:50
  3. problem with QTcpSocket
    By SuperSonik in forum Qt Programming
    Replies: 8
    Last Post: 31st January 2007, 16:00
  4. Problems with QThread and QTcpSocket
    By cookiem in forum Qt Programming
    Replies: 6
    Last Post: 2nd November 2006, 08:25
  5. Problem with QTcpSocket and QDataStream
    By Valheru in forum Qt Programming
    Replies: 4
    Last Post: 16th September 2006, 13:08

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.