Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QUdpSocket working on Ubuntu but not WIndows10



  • 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.



  • QThread is not the thread, it's a wrapper around the secondary thread living in the main thread. Slots of a QThread subclass are executed in the main thread.
    See https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/



  • Thank you for the information @VRonin
    I applied it to my implementation.


    Did anybody have a problem with porting QUdpSockets to Windows10 from Linux?


Log in to reply