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!