Results 1 to 5 of 5

Thread: QThread and event loop

  1. #1
    Join Date
    Mar 2015
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QThread and event loop

    Hello,

    I am a beginner in programming and i want to make an application to retieve data with UDP protocole (using QUdpSocket). I use a thread and I would like to manage event management in one.
    The problem is that I have an error and I can not find any doc about this error...

    Here is my server class Serveur.h:

    Qt Code:
    1. class Serveur : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit Serveur(QThread *parent = 0);
    6. Serveur(QString pAdr, int pPort);
    7.  
    8. void closeSocket();
    9. void run();
    10. private:
    11. QUdpSocket *socket;
    12.  
    13. QString Address;
    14. int Port;
    15. public slots:
    16. void read();
    17. void erreurSocket(QAbstractSocket::SocketError error);
    18.  
    19. };
    To copy to clipboard, switch view to plain text mode 

    Serveur.cpp:

    Qt Code:
    1. Serveur::Serveur(QString pAdr, int pPort)
    2. {
    3.  
    4. Address = pAdr;
    5. Port = pPort;
    6. socket = NULL;
    7. qRegisterMetaType<QAbstractSocket::SocketError>( "QAbstractSocket::SocketError" );
    8. host = new QHostAddress("192.168.1.33");
    9.  
    10. }
    11.  
    12. void Serveur::closeSocket()
    13. {
    14. this->quit();
    15.  
    16. if( socket != NULL )
    17. {
    18. socket->close();
    19. delete socket;
    20. socket = NULL;
    21. }
    22. delete host;
    23. host = NULL;
    24. }
    25.  
    26. void Serveur::run()
    27. {
    28. socket = new QUdpSocket();
    29.  
    30. connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
    31. connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(erreurSocket(QAbstractSocket::SocketError)));
    32. qDebug() << socket->thread();
    33.  
    34. socket->bind(QHostAddress(Address), Port);
    35.  
    36. exec();
    37. }
    38.  
    39. void Serveur::erreurSocket(QAbstractSocket::SocketError error)
    40. {
    41. switch( error )
    42. {
    43. case QAbstractSocket::HostNotFoundError:
    44. qDebug() << "Error : can't find the server. Verify ip and port.";
    45. break;
    46. case QAbstractSocket::ConnectionRefusedError:
    47. qDebug() << "Error : the server refused the connection or is not connected.";
    48. break;
    49. case QAbstractSocket::RemoteHostClosedError:
    50. qDebug() << "Error : the server close the connexion.";
    51. break;
    52. default:
    53. qDebug() << "Error : " + socket->errorString();
    54. }
    55. }
    56.  
    57. void Serveur::read()
    58. {
    59.  
    60. while(socket->hasPendingDatagrams())
    61. {
    62. QByteArray datagram;
    63. datagram.resize(socket->pendingDatagramSize());
    64.  
    65. quint16 senderPort = Port;
    66. quint64 lReceive = ( socket->readDatagram(datagram.data(), datagram.size(), host, &senderPort ) );
    67.  
    68. qDebug() << "Nb bytes : " << lReceive;
    69. }
    70. }
    To copy to clipboard, switch view to plain text mode 

    When my program starts all goes well, but sometimes it crashes with the following error message:
    QSocketNotifier: Invalid socket 15 and type 'Read', disabling...
    However, when I create my QUdpSocket object in the class constructor, and Event management is managed in the main thread, it works.

    Any ideas?

    Thank you in advance, and sorry for my bad english!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QThread and event loop

    First question, as usual when threads are used: are you sure you need to use one?

    Then a couple of suggestions:
    - do not delete the socket in the detructor, delete it after exec() returns, i.e. within the worker thread.
    - no need to make the QHostAddress a pointer
    - do not make internal slots public
    - be aware that your slots are currently executed in the main thread, so do not access "socket" in them

    Cheers,
    _

  3. #3
    Join Date
    Mar 2015
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread and event loop

    Thanks you for your quickly response.

    Yes i need to use a thread because the frequency of data's reception is important, otherwise it would block my application.
    I modified my code, but there is something I do not understand...

    " - be aware that your slots are currently executed in the main thread, so do not access "socket" in them " : effectively, i added
    Qt Code:
    1. qDebug() << this->thread();
    To copy to clipboard, switch view to plain text mode 
    and my slot run in the main thread. But, how I can retrieve data before the error, how can I access the socket?

    In the console, i can see :

    Nb byte : 936
    Nb byte : 936
    Nb byte : 936
    Nb byte : 936
    QSocketNotifier: Invalid socket 15 and type 'Read', disabling...
    Nb byte : 936
    Nb byte : 936
    Nb byte : 936
    Nb byte : 936
    Sometimes this error is not blocking

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QThread and event loop

    Quote Originally Posted by Crashz83 View Post
    Yes i need to use a thread because the frequency of data's reception is important, otherwise it would block my application.
    Ok, I was just checking that you had implemented it without threads first and only start using threads as a last resort.

    Quote Originally Posted by Crashz83 View Post
    I modified my code, but there is something I do not understand...

    " - be aware that your slots are currently executed in the main thread, so do not access "socket" in them " : effectively, i added
    Qt Code:
    1. qDebug() << this->thread();
    To copy to clipboard, switch view to plain text mode 
    and my slot run in the main thread. But, how I can retrieve data before the error, how can I access the socket?
    Well, you should not access the socket from two threads. So it would make most sense to handle the signals of the socket in the same thread as the socket.
    Either by having the receiver on the same thread or by forcing a direct call using Qt::DirectConnection as the connection type.

    Currently you have the receiver on the main thread and are using the default connection type (Qt::AutoConnection) which results in the behavior of Qt::QueuedConnection (emitter thread and receiver owner thread different).

    Cheers,
    _

  5. #5
    Join Date
    Mar 2015
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread and event loop

    It's ok, I simply added a call to Qt :: DirectConnexion in the connection of slots and everything works.

    Thanks you very much!!

Similar Threads

  1. QtWinMigrate and QThread event loop
    By dslee168 in forum Qt Programming
    Replies: 1
    Last Post: 8th September 2013, 15:37
  2. Replies: 25
    Last Post: 28th October 2011, 21:22
  3. Terminate a QThread with an event loop
    By paolom in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2010, 12:53
  4. QThread event loop blocking the GUI
    By JoeMerchant in forum Qt Programming
    Replies: 4
    Last Post: 18th July 2009, 08:54
  5. QThread event loop seems blocked
    By eurodatar in forum Qt Programming
    Replies: 3
    Last Post: 6th May 2009, 17:50

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.