Results 1 to 7 of 7

Thread: QThread vs QTcpServer feat. QTcpSocket

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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.


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

    ilyagoo (14th December 2009)

  3. #2
    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?

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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.


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