我正在尝试编写一个QT程序来接收UDP数据包。我正在尝试从Packet Sender软件接收 这是我的代码
socket = new QUdpSocket(this);
bool result = socket->bind(QHostAddress("150.100.50.88"),45454);
qDebug() << result;
if(result)
{
qDebug << "PASS";
}
else
{
qDebug << "FAIL";
}
processPendingDatagrams();
connect(socket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()),Qt::QueuedConnection);
void UDP::processPendingDatagrams()
{
QHostAddress sender;
u_int16_t port;
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;
} //! [2]
}
UDP.h:
class UDP : public QObject
{
Q_OBJECT public:
explicit UDP(QObject *parent = 0);
signals:
public slots:
void SendDatagram(u_int8_t,u_int8_t,u_int8_t);
private slots:
void processPendingDatagrams();
private :
QUdpSocket *socket;
};
readReady信号和相应的插槽不工作。我可以在Wireshark中看到数据包。 如果我尝试在循环中连续接收数据包,我可以看到数据报。这可能是信号和插槽无法工作的原因。发送操作运行良好。
答案 0 :(得分:3)
此代码适合我。试试吧。
的.pro:
#-------------------------------------------------
#
# Project created by QtCreator 2017-03-10T11:44:19
#
#-------------------------------------------------
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = test
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
mainwindow.cpp:
#include "mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
socket = new QUdpSocket(this);
bool result = socket->bind(QHostAddress::AnyIPv4, 45454);
qDebug() << result;
if(result)
{
qDebug() << "PASS";
}
else
{
qDebug() << "FAIL";
}
processPendingDatagrams();
connect(socket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()),Qt::QueuedConnection);
}
MainWindow::~MainWindow()
{
}
void MainWindow::processPendingDatagrams()
{
qDebug() << "in !";
QHostAddress sender;
u_int16_t port;
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;
}
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QUdpSocket>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void processPendingDatagrams();
private:
QUdpSocket *socket = nullptr;
};
#endif // MAINWINDOW_H
main.cpp中:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
尝试使用netcat命令:
netcat -u 127.0.0.1 45454
运行命令后,只需键入任何内容并按回车键。
答案 1 :(得分:0)
我正在尝试编写一个Qt应用程序,该应用程序使用Gstreamer管道播放从udp套接字接收的.mp4文件。 遇到同样的问题。
main.cpp
int main(int argc, char *argv[])`enter code here`
{
QApplication a(argc, argv);
QWidget *window = new QWidget;
QVideoWidget *videoWidget = new QVideoWidget;
QBoxLayout *layout = new QVBoxLayout;
QMediaPlayer *player = new QMediaPlayer;
QProcess *process = new QProcess;
layout->addWidget(videoWidget);
window->setLayout(layout);
window->show();
player->setVideoOutput(videoWidget);
QString program = "gst-launch-1.0";
QStringList arguments;
arguments << "udpsrc" << "port=34400" << "caps=application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)JPEG, payload=(int)26" << "!" << "rtpjpegdepay" << "!" << "jpegdec" << "!" << "filesink location=a.mp4";
process->setReadChannel(QProcess::StandardError);
process->start(program, arguments);
while (!process->waitForReadyRead()) {}
player->setMedia(QMediaContent(), process);
player->play();
return a.exec();
}
```