Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QUdpSocket working on Ubuntu but not WIndows10
Qt 6.11 is out! See what's new in the release blog

QUdpSocket working on Ubuntu but not WIndows10

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 528 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kioij
    wrote on last edited by kioij
    #1

    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.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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/

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • K Offline
        K Offline
        kioij
        wrote on last edited by kioij
        #3

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


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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved