Answer the question
In order to leave comments, you need to log in
QUdpSocket on a thread?
Good afternoon.
Eats a class launched in a separate thread, UDP sockets are created in it, and listened to.
Packets are coming, but the readyRead signal is not called on sockets.
h
class ReadImit : public QObject
{
Q_OBJECT
public:
explicit ReadImit(QObject *parent = 0);
signals:
void f1(s1Data);
void f2(s2Data);
public slots:
private slots:
void readPendingDatagramsIP1();
void readPendingDatagramsIP2();
private:
QUdpSocket* udpSocketIP1;
QUdpSocket* udpSocketIP2;
};
class ControlReadImit: public QThread
{
Q_OBJECT
public:
ControlReadImit()
{
qRegisterMetaType< s1Data> ("s1Data");
qRegisterMetaType< s2Data> ("s2Data");
}
signals:
// signal to thread
// signal from thread
void f1(s1Data);
void f2(s2Data);
private:
void run()
{
ReadImit threadReadImit;
// signal from thread
connect(&threadReadImit, SIGNAL(f1(s1Data)), this, SIGNAL(f1(s1Data)));
connect(&threadReadImit, SIGNAL(f2(s2Data)), this, SIGNAL(f2(s2Data)));
//исправление по комментарию
exec();
}
};
ReadImit::ReadImit(QObject *parent) :
QObject(parent)
{
udpSocketIP1 = new QUdpSocket(this);
udpSocketIP1->bind(QHostAddress::Any, PORT_IP1);
udpSocketIP2 = new QUdpSocket(this);
udpSocketIP2->bind(QHostAddress::Any, PORT_IP2);
connect(udpSocketIP1, SIGNAL(readyRead()), SLOT(readPendingDatagramsIP1()));
connect(udpSocketIP2, SIGNAL(readyRead()), SLOT(readPendingDatagramsIP2()));
}
void ReadImit::readPendingDatagramsIP1()
{
while (udpSocketIP1->hasPendingDatagrams()) {
......
}
}
void ReadImit::readPendingDatagramsIP2()
{
while (udpSocketIP2->hasPendingDatagrams()) {
......
}
}
Answer the question
In order to leave comments, you need to log in
Try specifying QUdpSocket::DontShareAddress as the third parameter in bind.
.. and it seems to me that you have a socket not created in a thread, or write without threads or moveToThread
It's simple, your run function is not included in the eventloop, but ends immediately after the connections.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question