Results 1 to 7 of 7

Thread: QThread vs QTcpServer feat. QTcpSocket

  1. #1
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    1

    Question QThread vs QTcpServer feat. QTcpSocket

    hi there! this is my first post let's start!

    i've read almost all posts about QThread, QTcpServer & QTcpSocket, but still have some problems.
    i wanna advice in the subject & all around

    the main idea is to write multithreaded app using QTcpServer & QTcpSocket.

    here's little bit of code:

    client class
    Qt Code:
    1. class TClient : public QTcpSocket
    2. {
    3. public:
    4. TClient()
    5. {
    6. connect( this, SIGNAL( connected() ), this, SLOT( connectedSlot() ) );
    7. connect( this, SIGNAL( disconnected() ), this, SLOT( disconnectedSlot() ) );
    8. }
    9.  
    10. private slots:
    11. void connectedSlot() { emit connected( this ); }
    12. void disconnectedSlot() { emit disconnected( this ); }
    13.  
    14. signals:
    15. void connected( TClient * );
    16. void disconnected( TClient * );
    17. };
    To copy to clipboard, switch view to plain text mode 

    thread class
    Qt Code:
    1. class TClientThread : public QThread
    2. {
    3. public:
    4. TClientThread( TClient *client ) : m_Client( client ) {}
    5. ~TClientThread()
    6. {
    7. wait();
    8. }
    9.  
    10. protected:
    11. void run()
    12. {
    13. connect( m_Client, SIGNAL( destroyed() ), this, SLOT( quit() ) );
    14. exec();
    15. }
    16.  
    17. private:
    18. TClient *m_Client;
    19. };
    To copy to clipboard, switch view to plain text mode 

    server class
    Qt Code:
    1. class TServer : public QTcpServer
    2. {
    3. protected:
    4. void incomingConnection( int socketDescriptor )
    5. {
    6. TClient *client = new TClient;
    7. // assuming no error
    8. client->setSocketDescriptor( socketDescriptor );
    9. connect( client, SIGNAL( connected( TClient * ) ), this, SLOT( clientConnected( TClient * ) ), Qt::QueuedConnection );
    10. connect( client, SIGNAL( disconnected( TClient * ) ), this, SLOT( clientDisconnected( TClient * ) ), Qt::QueuedConnection );
    11.  
    12. TClientThread *thread = new TClientThread( client );
    13. connect( thread, SIGNAL( finished() ), this, SLOT( killThread() ) );
    14. client->moveToThread( thread );
    15. thread->start();
    16.  
    17. emit clientConnected( client );
    18. }
    19.  
    20. signals:
    21. void clientConnected( TClient * );
    22. void clientDisconnected( TClient * );
    23.  
    24. private slots:
    25. void killThread()
    26. {
    27. delete sender();
    28. }
    29. };
    To copy to clipboard, switch view to plain text mode 

    i call client->deleteLater() in the slot connected to TServer::clientDisconnected( TClient * );

    1) is the 13th line in server class good idea or not?

    2) may be better to move thread creation & moving client to thread from incomingConnection() to the slot connected to TServer::clientDisconnected( TClient * ) signal?

    3) i need to obtain data from clientThread from other thread. data would be represented by char *[1-16 kBytes]? client receives data uninterruptedly, another thread process this data. i don't want client to be able to know anything about extern queue or mutex...
    do you know some design pattern to use in this case or with threads in general?

    4) i designed client anisochronous because i wanna use it either in main or spawned thread. it has relationship with 2)

    thanks for your attention

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread vs QTcpServer feat. QTcpSocket

    Quote Originally Posted by ilyagoo View Post
    1) is the 13th line in server class good idea or not?
    Bad idea. Never explicitely delete a sender of a signal inside slots connected to that signal. Use deleteLater() instead (it's a slot, by the way).

    2) may be better to move thread creation & moving client to thread from incomingConnection() to the slot connected to TServer::clientDisconnected( TClient * ) signal?
    The socket has to be created within the thread's run() method.

    3) i need to obtain data from clientThread from other thread. data would be represented by char *[1-16 kBytes]? client receives data uninterruptedly, another thread process this data. i don't want client to be able to know anything about extern queue or mutex...
    do you know some design pattern to use in this case or with threads in general?
    Emit a signal containing a byte array with the data.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    1

    Default Re: QThread vs QTcpServer feat. QTcpSocket

    Use deleteLater() instead (it's a slot, by the way).
    connect( thread, SIGNAL( finished() ), thread, SLOT( deleteLater() ) );
    thanks


    The socket has to be created within the thread's run() method.
    but how to use its functions? using signals? smth like this?

    Qt Code:
    1. TClientThread::run()
    2. {
    3. TClient client;
    4. client->setSocketDescriptor( socketDescriptor );
    5. // use client's functions via signals
    6. connect( this, SIGNAL( someThreadSignals ), &client, SLOT( someThreadSlots() ) );
    7.  
    8. exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    could i have any problems in thread's destructor relating to client on the stack?

    Emit a signal containing a byte array with the data
    to much copies of data: when i emit signal from one thread to another Qt produces QEvent with the copy of QByteArray. i receives 16kBytes about 100 times per second, its too hard to copy all of these arrays.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread vs QTcpServer feat. QTcpSocket

    Quote Originally Posted by ilyagoo View Post
    QEvent with the copy of QByteArray. i receives 16kBytes about 100 times per second, its too hard to copy all of these arrays.
    Copy of a byte array is not the same as copy of the data it carries. QByteArray is an implicitly shared class thus there is only one copy of the data in question.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    ilyagoo (14th December 2009)

  6. #5
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    1

    Default Re: QThread vs QTcpServer feat. QTcpSocket

    i.e. using of QByteArray instead of queues of char* wouldn't decrease app's perfomance?

    and would following code be correct to your mind?

    Qt Code:
    1. void TClient::readyReadSlot()
    2. {
    3. if ( !m_HeaderProcessed )
    4. {
    5. read( reinterpret_cast< char * >( &m_PackageHeader ), sizeof( m_PackageHeader ) );
    6. m_PackageHeaderProcessed = true;
    7. }
    8.  
    9. int bytesToRead = bytesAvailable();
    10.  
    11. if ( bytesToRead >= m_PackageHeader.MessageSize )
    12. {
    13. QByteArray message = read( m_PackageHeader.MessageSize );
    14. m_PackageHeaderProcessed = false;
    15.  
    16. emit dataReceived( message );
    17. }
    18. else
    19. // read remaining data
    20. emit readyRead();
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 


    and returning to:

    Qt Code:
    1. TClientThread::run()
    2. {
    3. TClient client;
    4. // little mistake))
    5. client.setSocketDescriptor( socketDescriptor );
    6. // use client's functions via signals
    7. connect( this, SIGNAL( someThreadSignals ), &client, SLOT( someThreadSlots() ) );
    8.  
    9. exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    i introduced signal TServer::clientDisconnected( TClient * ) because i need some information about disconnected client, for example, peerAddress & peerPort. this suggests me to use following code:

    Qt Code:
    1. TClientThread::run()
    2. {
    3. m_Client = new TClient;
    4. m_Client->setSocketDescriptor( socketDescriptor );
    5. // use client's functions via signals
    6. connect( this, SIGNAL( someThreadSignals ), m_Client, SLOT( someThreadSlots() ) );
    7.  
    8. exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    but how can i finish thread correct now?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread vs QTcpServer feat. QTcpSocket

    Quote Originally Posted by ilyagoo View Post
    i.e. using of QByteArray instead of queues of char* wouldn't decrease app's perfomance?
    No. At least not in any noticable way.

    and would following code be correct to your mind?
    The first "if" block is very C-ish. IMO you should read the header into a byte array. The rest seems fine.

    but how can i finish thread correct now?
    I don't see your point...

    By the way, are you sure you need threads at all? So far I see no reason to use them in your situation.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    1

    Default Re: QThread vs QTcpServer feat. QTcpSocket

    returning to my phrase:
    Qt Code:
    1. i introduced signal TServer::clientDisconnected( TClient * ) because i need some information about disconnected client, for example, peerAddress & peerPort.
    To copy to clipboard, switch view to plain text mode 

    would it be correct to call client->peerPort() in the slot connected to aforesaid signal if this call and client are in different threads?

Similar Threads

  1. QTcpSocket as class member of QThread Issue
    By zyangxue in forum Qt Programming
    Replies: 4
    Last Post: 12th December 2009, 06:42
  2. QThread and QTcpSocket
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 19th May 2008, 17:43
  3. QThread and QTcpSocket
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2008, 13:06
  4. QTcpServer & QTcpSocket questions...
    By jxmot in forum Qt Programming
    Replies: 2
    Last Post: 24th April 2008, 21:38
  5. Problem with QTcpSocket in QThread
    By Raistlin in forum Qt Programming
    Replies: 8
    Last Post: 6th October 2007, 12:23

Tags for this Thread

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.