QT UDP socket does not recive any thing

Viewed 36

I have a STM32 and it would send and receive UDP packets successfully with hercules_3-2-8. Now I have this simple code in QT, it can send UPD packets, But it would not receive any thing, the readyRead slot would never be called,

MyUDP::MyUDP(QObject *parent) : QObject(parent)
{
    socket = new QUdpSocket(this);
    socket->bind(QHostAddress("192.168.1.100"),2000);
    connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
}


void MyUDP::SayHello(){
    QByteArray Data;
    Data.append("Hello from ASiDesigner");
    socket->writeDatagram(Data,QHostAddress("192.168.1.100"),2000);
    qDebug()<<"Hi Nitrogen";
}




void MyUDP::readyRead(){
    QByteArray Buffer;
    Buffer.resize((socket->pendingDatagramSize()));

    QHostAddress sender;
    quint16 senderPort;


    socket->readDatagram(Buffer.data(),Buffer.size(),&sender,&senderPort);

    qDebug()<<"Message from: "<<sender.toString();
    qDebug()<<"Message port: "<<senderPort;
    qDebug()<<"Message: "<<Buffer;

}

The mainwindow.cpp file content

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "myudp.h"

MyUDP Server;
MyUDP client;


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);


}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    client.SayHello();

}
1 Answers

The problem was solved by moving all the related UDP functions into the mainwindow.cpp file, Maybe it's related to priories in QT!

Related