简体   繁体   English

为什么我的 class 没有使用 Qt QUDPSocket 接收 UDP 数据报?

[英]Why my class doesn't receive UDP datagrams with the Qt QUDPSocket?

I'm trying to develop a UDP datagram receiver to read packets from a UDP server that update information via UDP datagrams.我正在尝试开发一个 UDP 数据报接收器来从 UDP 服务器读取数据包,该服务器通过 UDP 数据报更新信息。 I want to receive the datagrams and after update the data reading the payload.我想接收数据报并在更新数据后读取有效负载。 I followed the Qt Tutorial Example for developing a Multicast Receiver.我按照 Qt 教程示例来开发多播接收器。 I just copied the code, but, while the example receives and read the datagram, the same code in my application does not.我只是复制了代码,但是,虽然示例接收并读取数据报,但我的应用程序中的相同代码却没有。 It does not want to work.它不想工作。 What I'm getting wrong?我错了什么?

here is the code of the class I deveoloped:这是我开发的 class 的代码:

UDPDataReceiver.h UDPDataReceiver.h

class UDPDataReceiver: public QObject
{
    Q_OBJECT

public:
    explicit UDPDataReceiver(QObject *parent = nullptr);

public slots:
    void readPendingDatagrams();

private:
    QUdpSocket m_socket;
    QHostAddress groupAddress4;

};

UDPDataReceiver.cpp UDPDataReceiver.cpp

UDPDataReceiver::UDPDataReceiver(QObject *parent) : QObject(parent),
    groupAddress4(QStringLiteral("234.5.6.7"))
{
    const QHostAddress &localhost = QHostAddress(QHostAddress::LocalHost);
//    for (const QHostAddress &address: QNetworkInterface::allAddresses()) {
//        if (address.protocol() == QAbstractSocket::IPv4Protocol && address != localhost)
//             qDebug() << address.toString();
//    }

    bool bound = m_socket.bind(localhost, 2471, QUdpSocket::ShareAddress);
    bool joined = m_socket.joinMulticastGroup(groupAddress4);
    connect(&m_socket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
}

void UDPDataReceiver::readPendingDatagrams()
{
    QByteArray datagram;

    while (m_socket.hasPendingDatagrams()) {
        datagram.resize(int(m_socket.pendingDatagramSize()));
        m_socket.readDatagram(datagram.data(), datagram.size());
       qDebug()<<datagram.constData()<<"Example implementation";
    }
}

In the constructor of my MainWindow class I call the code that follow to create an instance of the receiver.在我的 MainWindow class 的构造函数中,我调用以下代码来创建接收器的实例。

 dataReceiver = new UDPDataReceiver(this);

Trying to run the Qt example of the multicast receiver (https://doc.qt.io/qt-5/qtnetwork-multicastreceiver-example.html ) it reads well the datagrams. Trying to run the Qt example of the multicast receiver (https://doc.qt.io/qt-5/qtnetwork-multicastreceiver-example.html ) it reads well the datagrams. With the same code in my application, nothing had been read.在我的应用程序中使用相同的代码,没有读取任何内容。

Thanks to whom will help me.感谢谁会帮助我。

I'll try to help you with a couple of tips.我将尝试为您提供一些提示。

If You use Windows You need to switch off your firewall or add your application to it's list.如果您使用 Windows 您需要关闭防火墙或将您的应用程序添加到它的列表中。 Next step.下一步。 You can try to switch your variable QUdpSocket m_socket into QUdpSocket *m_socket .您可以尝试将变量QUdpSocket m_socket切换为QUdpSocket *m_socket And try yo use cycle do{}while() like this code:并尝试使用循环do{}while()像这样的代码:

do
{
    datagram.resize(int(m_socket.pendingDatagramSize()));
    m_socket.readDatagram(datagram.data(), datagram.size());
    qDebug()<< datagram.constData() <<"Example implementation";
}while(m_socket.hasPendingDatagrams());

Maybe the condition is not met, but on the second cycle it will probably work!也许条件不满足,但在第二个周期它可能会起作用!

Let's try the best, my friend!让我们努力吧,我的朋友!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM