Results 1 to 5 of 5

Thread: QThread + QTcpServer

  1. #1
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default QThread + QTcpServer

    There is a following code:


    Qt Code:
    1. class Connection : public QTcpSocket
    2. {
    3. Q_OBJECT
    4. public:
    5. Connection(QObject *parent = 0);
    6. private slots:
    7. void processReadyRead();
    8. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. Connection::Connection(QObject *parent)
    2. : QTcpSocket(parent)
    3. {
    4.  
    5. QObject::connect(this, SIGNAL(readyRead()), this, SLOT(processReadyRead()));
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Connection::processReadyRead()
    2. {
    3. char *szData = 0;
    4. if( this->isReadable())
    5. {
    6. qint64 qnSize = this->bytesAvailable();
    7. if( qnSize > 0)
    8. {
    9. szData = new char [qnSize];
    10. qint64 qnReadSize = this->read( szData, qnSize);
    11. szData[qnReadSize]=0;
    12. }
    13. }
    14. Ui_HSMClass *pHSMClass = CProjectManager::GetPrjMng()->m_pHSMClass;
    15. if(pHSMClass != NULL)
    16. {
    17. pHSMClass->addMSG(szData);
    18. }
    19. this->disconnectFromHost();
    20. this->waitForDisconnected();
    21. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. class FortuneThread : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. FortuneThread(int socketDescriptor,QObject *parent);
    6. void run();
    7. signals:
    8. void error(QTcpSocket::SocketError socketError);
    9. private:
    10. int socketDescriptor;
    11. };
    To copy to clipboard, switch view to plain text mode 



    Qt Code:
    1. FortuneThread::FortuneThread(int socketDescriptor,QObject *parent)
    2. : QThread(parent), socketDescriptor(socketDescriptor)
    3. {
    4.  
    5. }
    6. void FortuneThread::run()
    7. {
    8. Connection *connection = new Connection(this);
    9. connection->setSocketDescriptor(socketDescriptor);
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 



    Qt Code:
    1. class Server : public QTcpServer
    2. {
    3. Q_OBJECT
    4. public:
    5. Server(QObject *parent = 0);
    6. signals:
    7. void newConnection(Connection *connection);
    8. protected:
    9. void incomingConnection(int socketDescriptor);
    10.  
    11. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. Server::Server(QObject *parent)
    2. : QTcpServer(parent)
    3. {
    4. if (!listen(QHostAddress::Any,1718)) {
    5. return;
    6. }
    7. }
    8.  
    9. void Server::incomingConnection(int socketDescriptor)
    10. {
    11. FortuneThread *thread = new FortuneThread(socketDescriptor,this);
    12. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    13. thread->start();
    14. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. class HSM : public QDialog
    2. {
    3. Q_OBJECT
    4. public:
    5. CProjectManager *m_pPrj;
    6. HSM(QWidget *parent = 0, Qt::WFlags flags = 0);
    7. ~HSM();
    8. private:
    9. Ui_HSMClass ui;
    10. Server server;
    11. private slots:
    12. void on_pushButton_clicked();
    13.  
    14. };
    To copy to clipboard, switch view to plain text mode 



    The client connect to a server, but function processReadyRead() does not work ..Why ???
    can help me ???
    Last edited by Fastman; 19th July 2007 at 15:33.

  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: QThread + QTcpServer

    Could you please change "qtclass" tags to "code" to make it readable?

    PS. It's the button labeled "#". Button labeled "Qt" is for referencing to Qt docs.
    J-P Nurmi

  3. #3
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QThread + QTcpServer

    sorry

  4. #4
    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: QThread + QTcpServer

    Quote Originally Posted by Fastman View Post
    Qt Code:
    1. void FortuneThread::run()
    2. {
    3. Connection *connection = new Connection(this);
    4. connection->setSocketDescriptor(socketDescriptor);
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 
    This should produce a warning in the debug output. Every QObject has a tread affinity. You cannot create a QObject with a parent "living in" a different thread. FortuneThread object itself lives in the main GUI thread (where it was created), which is different thread than what's being executed in FortuneThread::run(). Also, QThread finishes by the time execution returns from QThread::run() so in your case this happens immediately.

    Give it a try with something like this:
    Qt Code:
    1. void FortuneThread::run()
    2. {
    3. Connection connection;
    4. connection.setSocketDescriptor(socketDescriptor);
    5. connect(&connection, SIGNAL(disconnected()), this, SLOT(quit()));
    6. exec(); // blocks as long as the event loop runs
    7. }
    To copy to clipboard, switch view to plain text mode 

    PS. Thanks for reformatting the code
    J-P Nurmi

  5. #5
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QThread + QTcpServer

    Jpn !
    thanks, also excuse for my English, I from Minsk

Similar Threads

  1. QThread exec proplem to stop...
    By patrik08 in forum Qt Programming
    Replies: 29
    Last Post: 21st May 2007, 07:51
  2. QGraphicsScene and QThread
    By tts80 in forum Qt Programming
    Replies: 5
    Last Post: 10th January 2007, 09:32
  3. how to use QHttp inside QThread in Qt3
    By alusuel in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2006, 11:19
  4. Replies: 3
    Last Post: 11th May 2006, 00:00
  5. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 22:51

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
  •  
Qt is a trademark of The Qt Company.