Can I use QUdpSockets wo polling or custom classes

2019-06-12 09:53发布

问题:

Here is a short UDP server example in Qt below which does work but what I don't like is that I'm polling to see if new data is available. I've come across some examples of a readyRead() but they all seem to introduce a qt class. Do I need to use a qt class in order to take advantage of the readyRead() signal?

Here is the working but simple UDP server implemented entirely in main:

#include <QDebug>
#include <QUdpSocket>
#include <QThread>

int main(int argc, char *argv[])
{
    QUdpSocket *socket = new QUdpSocket();
    u_int16_t port = 7777;

    bool bindSuccess =  socket->bind(QHostAddress::AnyIPv4, port);
    if (!bindSuccess) {
        qDebug() << "Error binding to port " << port << " on local IPs";
        return a.exec();
    }
    qDebug() << "Started UDP Server on " << port << endl;

    QHostAddress sender;
    while (true) {
        while (socket->hasPendingDatagrams()) {
            QByteArray datagram;
            datagram.resize(socket->pendingDatagramSize());
            socket->readDatagram(datagram.data(),datagram.size(),&sender,&port);
            qDebug() << "Message From :: " << sender.toString();
            qDebug() << "Port From :: "<< port;
            qDebug() << "Message :: " << datagram.data();
        }
        QThread::msleep(20);
    }

    return 0;
}

Here is an example of the readyRead() signal: https://www.bogotobogo.com/Qt/Qt5_QUdpSocket.php

I haven't really figured out how to get this to work yet. I must be doing something wrong. Here is the UDP connection code i'm trying:

#include "myudp.h"

MyUDP::MyUDP(QObject *parent) : QObject(parent) {
}

void MyUDP::initSocket(u_int16_t p) {
    port = p;

    udpSocket = new QUdpSocket(this);
    bool bindSuccess = udpSocket->bind(QHostAddress::LocalHost, port);
    if (!bindSuccess) {
        qDebug() << "Error binding to port " << port << " on local IPs";
        return;
    }
    connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
}

void MyUDP::readPendingDatagrams() {
    QHostAddress sender;
    while (udpSocket->hasPendingDatagrams()) {
        QByteArray datagram;
        datagram.resize(udpSocket->pendingDatagramSize());
        udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &port);
        qDebug() << "Message From :: " << sender.toString();
        qDebug() << "Port From :: " << port;
        qDebug() << "Message :: " << datagram.data();
    }
}

myudp.h

#include <QObject>
#include <QUdpSocket>

class MyUDP : public QObject
{
    Q_OBJECT
public:
    explicit MyUDP(QObject *parent);
    void initSocket(u_int16_t p);

    u_int16_t port;
    QUdpSocket *udpSocket;

signals:

public slots:
    void readPendingDatagrams();
};

new main.cpp

int main(int argc, char *argv[]) 
{
    MyUDP *myUDP = new MyUDP(0);
    myUDP->initSocket(port);

    while (true) {
        usleep(1000);
    }

    return 0;
}

I am testing with:

netcat 127.0.0.1 -u 7777
{"cid"="0x1234123412341", "fill_level"=3245 }<cr>

回答1:

What you're doing wrong is that you're not letting Qt's event loop run. i.e. this is incorrect:

int main(int argc, char *argv[]) 
{
   MyUDP *myUDP = new MyUDP(0);
   myUDP->initSocket(port);

   while (true) {
       usleep(1000);
   }

   return 0;
}

... instead, you should have something like this:

int main(int argc, char *argv[]) 
{
   QApplication app(argc, argv);

   // connect needs to occur after QCoreApplication declaration
   MyUDP *myUDP = new MyUDP(0);
   myUDP->initSocket(port);

   return app.exec();
}

... it is inside the app.exec() call where a Qt application spends most of its time (app.exec() won't return until Qt wants to quit), and there is where Qt will handle your UDP socket's I/O and signaling needs.



标签: c++ qt udp