Crashz83
17th August 2015, 14:29
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:
class Serveur : public QThread
{
Q_OBJECT
public:
explicit Serveur(QThread *parent = 0);
Serveur(QString pAdr, int pPort);
void closeSocket();
void run();
private:
QUdpSocket *socket;
QHostAddress* host;
QString Address;
int Port;
public slots:
void read();
void erreurSocket(QAbstractSocket::SocketError error);
};
Serveur.cpp:
Serveur::Serveur(QString pAdr, int pPort)
{
Address = pAdr;
Port = pPort;
socket = NULL;
qRegisterMetaType<QAbstractSocket::SocketError>( "QAbstractSocket::SocketError" );
host = new QHostAddress("192.168.1.33");
}
void Serveur::closeSocket()
{
this->quit();
if( socket != NULL )
{
socket->close();
delete socket;
socket = NULL;
}
delete host;
host = NULL;
}
void Serveur::run()
{
socket = new QUdpSocket();
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(erreurSocket(QAbstractSocket::SocketError)));
qDebug() << socket->thread();
socket->bind(QHostAddress(Address), Port);
exec();
}
void Serveur::erreurSocket(QAbstractSocket::SocketError error)
{
switch( error )
{
case QAbstractSocket::HostNotFoundError:
qDebug() << "Error : can't find the server. Verify ip and port.";
break;
case QAbstractSocket::ConnectionRefusedError:
qDebug() << "Error : the server refused the connection or is not connected.";
break;
case QAbstractSocket::RemoteHostClosedError:
qDebug() << "Error : the server close the connexion.";
break;
default:
qDebug() << "Error : " + socket->errorString();
}
}
void Serveur::read()
{
while(socket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(socket->pendingDatagramSize());
quint16 senderPort = Port;
quint64 lReceive = ( socket->readDatagram(datagram.data(), datagram.size(), host, &senderPort ) );
qDebug() << "Nb bytes : " << lReceive;
}
}
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! :)
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:
class Serveur : public QThread
{
Q_OBJECT
public:
explicit Serveur(QThread *parent = 0);
Serveur(QString pAdr, int pPort);
void closeSocket();
void run();
private:
QUdpSocket *socket;
QHostAddress* host;
QString Address;
int Port;
public slots:
void read();
void erreurSocket(QAbstractSocket::SocketError error);
};
Serveur.cpp:
Serveur::Serveur(QString pAdr, int pPort)
{
Address = pAdr;
Port = pPort;
socket = NULL;
qRegisterMetaType<QAbstractSocket::SocketError>( "QAbstractSocket::SocketError" );
host = new QHostAddress("192.168.1.33");
}
void Serveur::closeSocket()
{
this->quit();
if( socket != NULL )
{
socket->close();
delete socket;
socket = NULL;
}
delete host;
host = NULL;
}
void Serveur::run()
{
socket = new QUdpSocket();
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(erreurSocket(QAbstractSocket::SocketError)));
qDebug() << socket->thread();
socket->bind(QHostAddress(Address), Port);
exec();
}
void Serveur::erreurSocket(QAbstractSocket::SocketError error)
{
switch( error )
{
case QAbstractSocket::HostNotFoundError:
qDebug() << "Error : can't find the server. Verify ip and port.";
break;
case QAbstractSocket::ConnectionRefusedError:
qDebug() << "Error : the server refused the connection or is not connected.";
break;
case QAbstractSocket::RemoteHostClosedError:
qDebug() << "Error : the server close the connexion.";
break;
default:
qDebug() << "Error : " + socket->errorString();
}
}
void Serveur::read()
{
while(socket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(socket->pendingDatagramSize());
quint16 senderPort = Port;
quint64 lReceive = ( socket->readDatagram(datagram.data(), datagram.size(), host, &senderPort ) );
qDebug() << "Nb bytes : " << lReceive;
}
}
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! :)