5
votes

I am trying write a QT program to receive UDP packet.I am trying to receive from Packet Sender software This is my code

    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; 
 };

The readReady signal and corresponding slot are not working . I can see the packets in Wireshark. If a I try receive the packets in continuously in a loop I am able see the datagrams.What can be the reason for signals and Slots for not working.Sending operation is working well.

2
is your void UDP::processPendingDatagrams() definition in your .h in the slots instead of just the functions ? (with public/protected/private slots: )Gabriel de Grimouard
void UDP::processPendingDatagrams() is declare as public function not as slot.I have added the class to my questionKarthik Poojary
if you want to use it as a slot like you do in connect, you should move it to the slots. If it's as a function it's won't be called because Qt won't be able to link it with a signal.Gabriel de Grimouard
I have moved the declaration to Slots even after that there is no change.In wireshark I am getting Destination Unreachable (port unreachable).Karthik Poojary
if oyu are receiving the packets here, you should change the ip to QHostAddress::AnyIPv4. and try another port. (if you are on linux, you can try sending udp packets through netcat with nc)Gabriel de Grimouard

2 Answers

7
votes

This code work for me. Try it please.

.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();
}

tried with netcat with the command:

 netcat -u 127.0.0.1 45454

once you ran the command, just type anything and press return.

0
votes

I am trying to write a Qt app which play .mp4 file receive from udp socket using Gstreamer pipeline. having same issue.

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();
}
```