QUdpSocket working on Ubuntu but not WIndows10
-
wrote on 11 Jun 2020, 03:44 last edited by kioij 6 Nov 2020, 03:45
Hello everyone,
After looking through the forum, I did not see a solution to this particular problem.
I am having trouble with sending UDP packets through to Qt on Windows10.For sending a UDP packet, I'm using PacketSender from https://packetsender.com/.
Using this program, TCP packets are picked up by wireshark but UDP packets are not, even with the same IP address and local port.For the code, QUdpSocket is running on a separate thread.
It works on Ubuntu but the same code does not work in Windows 10.
I disabled Windows Firewall and also added a new inbound rule to unblock the local port for UDP but this doesn't seem to solve the problem.My Qt code for receiving the UDP packets is:
MySocket.h :
class MyUDPSocket: public QThread { Q_OBJECT public: MyUDPSocket(moodycamel::BlockingReaderWriterQueue<QByteArray>& buffer); void run(); public slots: void readPackets(); private: QUdpSocket *udpSkt; moodycamel::BlockingReaderWriterQueue<QByteArray>& queue; };
MySocket.cpp:
MyUDPSocket::MyUDPSocket(moodycamel::BlockingReaderWriterQueue<QByteArray>& buffer) : queue(buffer) { moveToThread(this); } void MyUDPSocket::run() { udpSkt = new QUdpSocket(this); udpSkt->bind(QHostAddress::Any, 56666); connect(udpSkt, SIGNAL(readyRead()), this, SLOT(readPackets())); exec(); } void MyUDPSocket::readPackets() { while (udpSkt->hasPendingDatagrams()) { QByteArray datagram; datagram.resize(udpSkt->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; udpSkt->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort); if(datagram.size() > 0) { queue.enqueue(datagram); } } }
Thank you for your help.
-
wrote on 11 Jun 2020, 07:42 last edited by
QThread
is not the thread, it's a wrapper around the secondary thread living in the main thread. Slots of aQThread
subclass are executed in the main thread.
See https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
1/3