最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - How do I read a payload to a different IP with QUdpSocket? - Stack Overflow

programmeradmin1浏览0评论

I need to read the payload of a UDP message transmitted by a server with IP 172.19.66.100 with port 50493, to another server with IP 239.9.66.100 with port 1000.

With Wireshark I can read it, but I am not able to when using QUdpSocket.

This is my code, based on the standard Qt example:

#include <QLabel>
#include <QPushButton>
#include <QUdpSocket>
#include <QVBoxLayout>
#include <QNetworkDatagram>

#include "receiver.h"

Receiver::Receiver(QWidget *parent)
    : QWidget(parent)
{
    statusLabel = new QLabel(tr("Listening for broadcasted messages"));
    statusLabel->setWordWrap(true);

    auto quitButton = new QPushButton(tr("&Quit"));

//! [0]
    udpSocket = new QUdpSocket(this);
    udpSocket->bind(QHostAddress("239.9.66.100"), 1000);
//! [0]

//! [1]
    connect(udpSocket, &QUdpSocket::readyRead,
            this, &Receiver::processPendingDatagrams);
//! [1]
    connect(quitButton, &QPushButton::clicked,
            this, &Receiver::close);

    auto buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch(1);
    buttonLayout->addWidget(quitButton);
    buttonLayout->addStretch(1);

    auto mainLayout = new QVBoxLayout;
    mainLayout->addWidget(statusLabel);
    mainLayout->addLayout(buttonLayout);
    setLayout(mainLayout);

    setWindowTitle(tr("Broadcast Receiver"));
}

void Receiver::processPendingDatagrams()
{
//! [2]
    while (udpSocket->hasPendingDatagrams()) {
        QNetworkDatagram datagram = udpSocket->receiveDatagram();
        statusLabel->setText(QString(datagram.data().toHex()));
//! [2]
    }
}

I guess the problem is in this line:

udpSocket->bind(QHostAddress("239.9.66.100"), 1000);

I tried different solutions but none is really working. The only way I managed to read the message, is by transmitting the message directly to my local IP, instead of the server IP, but it is not what I need.

How should I modify my code?

发布评论

评论列表(0)

  1. 暂无评论