QUdpSocket not emiting readyRead in a multi files project
-
The program is to take user input and send it via udp, and the receiver sends acknowledgement. Tried to read the acknowledgement udp using the readyRead signal. But, the signal is not emitted on arrival of package. Checked the status of bind() and it returns true, and the arrival of packet is confirmed through wireshark.
I'm not really sure if the connect() for readyRead is always in scope (as its written within the constructor for udp class).//udp.h #ifndef UDP_H #define UDP_H #include <QObject> #include <QUdpSocket> class udp : public QObject { Q_OBJECT public: explicit udp(QObject *parent = nullptr); void udpSend(); signals: public slots: void readUDP(); private: QUdpSocket *socket; }; #endif // UDP_H //udp.cpp #include "udp.h" #include "QDebug" #include "QUdpSocket" udp::udp(QObject *parent) : QObject{parent} { socket = new QUdpSocket(this); socket->bind(QHostAddress("192.168.43.209"), 1234); if(socket->state() == QUdpSocket::BoundState) { qDebug() << "server bound"; } else qDebug()<<"not bound"; bool stat = connect(socket, SIGNAL(readyRead()), this, SLOT(readUDP())); qDebug()<<"connect status"<<stat; } void udp::udpSend() { QByteArray buffer; buffer.append("Hello hey ..... \n I'm up and running"); socket->writeDatagram(buffer, QHostAddress("192.168.43.1"), 1234 ); } void udp::readUDP() { if(socket->hasPendingDatagrams()) qDebug()<<"data received"; QByteArray Buffer; Buffer.resize(socket->pendingDatagramSize()); QHostAddress sender; quint16 port; socket->readDatagram(Buffer.data(), Buffer.size(), &sender, &port); qDebug()<<"message is"<<Buffer; } //adduser.cpp (where the object is created and send is called) #include "adduser.h" #include "ui_adduser.h" #include "QMessageBox" #include "QDebug" #include "udp.h" addUser::addUser(QWidget *parent) : QDialog(parent), ui(new Ui::addUser) { ui->setupUi(this); } addUser::~addUser() { delete ui; } void addUser::on_pushButton_2_clicked() //submit button press { udp myudp; //UDP object creation addUser User; QMessageBox mssgBox; addUserName = ui->lineEdit->text(); addNewID = ui->lineEdit_2->text(); addRFID = ui->lineEdit_3->text(); IP = ui->lineEdit_4->text(); port = ui->lineEdit_5->text(); portNum = port.toInt(); int rfidLength = addRFID.length(); int lengthOfName = addUserName.length(); strLength = QString::number(lengthOfName); // number to string if((rfidLength == 10) && (lengthOfName <11)) { mssgBox.setWindowTitle("Confirmation"); mssgBox.setText("Confirm the new user details" ); mssgBox.setInformativeText("\n username: "+addUserName+"\n user ID: "+addNewID+"\n RFID: "+addRFID); mssgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Save); int returnValue = mssgBox.exec(); // QDeadlineTimer deadline(5000); switch(returnValue) { case QMessageBox::Save : { //packetize and send the details over UDP packetToDevice = "FE0A"+addNewID+strLength+addUserName+addRFID+"0B"; qDebug()<<"packet generated"; myudp.udpSend(); //(packetToDevice, IP, portNum); //UDP SEND Fn CALL qDebug()<<"user pressed save"; User.show(); // back to the window break; } case QMessageBox::Cancel : //continue to the user addition qDebug()<<"user pressed cancel"; User.show(); //back to window break; default: qDebug()<<"entered default state"; } qDebug()<<addUserName; qDebug()<<packetToDevice; } else { QMessageBox falseMssg; falseMssg.setWindowTitle("Error"); falseMssg.setText("Invalid RFID/Name"); falseMssg.setInformativeText("RFID must be 10 characters and Name should contain up to 10 characters"); falseMssg.setStandardButtons(QMessageBox::Close); falseMssg.exec(); addUser User; User.show(); } }
I've tried to create an object in the main.cpp and then create another one in the adduser.cpp to call the sending function, but the issue still persists.
The same udp source and header when used in another project with just a main.cpp file and object creation in main.cpp works fine
-
@febinaf said in QUdpSocket not emiting readyRead in a multi files project:
I've tried to create an object in the main.cpp and then create another one in the adduser.cpp to call the sending function, but the issue still persists.
I'm not sure what you are doing here. In
addUser::on_pushButton_2_clicked()
you create a localudp myudp; //UDP object creation
and send on that, but that goes out of scope at the end of that function. How do you expect to receive the datagrams then?If you create a
udp
instance somewhere you should make it available to wherever it is to be used. You might do that by passing it as a parameter toaddUser
class, or as a singleton or whatever, but I don't think you want to be creating more than one instance as it encapsulates your UDP socket.Also, separately but similarly, at least in the error case I notice
addUser User; User.show();
That creates a new
addUser
instance insideaddUser::on_pushButton_2_clicked()
which is already anadduser
instance. Then it shows it, which requires it to persist, but it immediately goes out of scope and gets destroyed. -
@febinaf To add to @JonB : please read https://en.cppreference.com/w/cpp/language/scope
-
@JonB said in QUdpSocket not emiting readyRead in a multi files project:
ooh ok, but I'm clueless on how to create a udp object in main (so that the readyRead is connected throughout the runtime), and use the same object in adduser file for sending over udp. I'll go through the suggested solutions.passing it as a parameter to addUser class
could you please explain a bit more on passing the object to the class, will that help me to keep the object in scope throughout the adduser file ?
addUser User; User.show();
I've used the above snippet, so that the window appears again after the QMessageBox is closed. Without that, the window used to close immediately after the message box is closed. I'm trying to take the user input again after the 'Save' or 'Cancel' button is clicked. Any info regarding the proper implementation of this idea would be really helpful.
really appreciate the time :)
-
@febinaf said in QUdpSocket not emiting readyRead in a multi files project:
how to create a udp object in main
Why in main?!
Simply make it class member... -
@jsulm Hey I've added the object for udp class in the addUser class. I've also forward declared the udp class in addUser header file. But I'm getting the error that " field 'myudp' has incomplete type 'myUDP'.
Here is the changes that I've made.//addUser.h #ifndef ADDUSER_H #define ADDUSER_H #include <QMainWindow> #include <QDialog> #include <QString> namespace Ui { class addUser; } class myUDP; //fwd declared class addUser : public QDialog { Q_OBJECT public: explicit addUser(QWidget *parent = nullptr); ~addUser(); QString packetToDevice, IP, port; int portNum; myUDP myudp; //object added as member private slots: void on_pushButton_2_clicked(); private: Ui::addUser *ui; QString addUserName, addNewID, addRFID, confirmBuffer, strLength; }; #endif // ADDUSER_H
and did #include "myudp.h" in adduser.cpp .
Also, tried to create
myUDP *myudp; //object pointer
and added, below line in addUser constructor
myudp = new myUDP(this);
and doing so removed the errors but, the socket -> bind() in myUDP constructor returns false during operation, and the readyRead is not emitted. Could you help me with what I'm missing here.
-
And where did you define + implement myUDP class?
Forward declaring an object is not possible. You can use a forward-declared class only as pointer. -
@Christian-Ehrlicher I've defined the myUDP class in myudp.cpp and used in in the addUser.cpp file.
I did all these steps to use the myudp object as a member of the addUser class. If this is not the right thing to do, could you guide me through the steps.
-
@febinaf said in QUdpSocket not emiting readyRead in a multi files project:
If this is not the right thing to do, could you guide me through the steps.
Forward declaring an object is not possible. You can use a forward-declared class only as a pointer.
I already told you whats wrong
-
@Christian-Ehrlicher
I'm really getting into object oriented programming and pardon me if I'm asking a lot of silly questions.I'm trying to achieve what @jsulm mentioned as "Simply make it class member..." . I've created an object in the addUser.h private sections and got errors, that's the reason for trying out forward declaration.
What I'm trying to obtain is, to make the signal readyRead() to be connected throughout the runtime, and the connect () is in the constructor of myUDP class. -
@febinaf said in QUdpSocket not emiting readyRead in a multi files project:
I've created an object in the addUser.h private sections and got errors
Then please post the error messages. What you did wrong with the forward declared class was already explained...
-
@Christian-Ehrlicher The error was that "the class name does not name a type" .. i think it was because of not including the required header files. Got that fixed now.
Right now I've created an object in private section of addUser.h and then I'm calling a method from that object in between the addUser GUI. I'm using a debug message to get status of socket ->bind(), and it returns true as the window opens the addUser gui, and the same returns false when the sendUDP method is called inside the same window.
Is this the reason for readyRead not being emitted. -
Please post a minimal, compilable exmaple to your problem - you did so many changes, posted only parts of your code so that I don't know what you are actually really compiling...
-
@Christian-Ehrlicher Ok
an on_pushButton_clicked from my mainwindow invokes an addUser object and shows the window.//addUser.h #ifndef ADDUSER_H #define ADDUSER_H #include <QMainWindow> #include <QDialog> #include <QString> #include <myudp.h> namespace Ui { class addUser; } class addUser : public QDialog { Q_OBJECT public: explicit addUser(QWidget *parent = nullptr); ~addUser(); QString packetToDevice, IP, port; int portNum; private slots: void on_pushButton_2_clicked(); private: Ui::addUser *ui; QString addUserName, addNewID, addRFID, confirmBuffer, strLength; myUDP myudp; //object added as member }; #endif // ADDUSER_H
//addUser.cpp #include "adduser.h" #include "ui_adduser.h" #include "QMessageBox" #include "QDebug" #include "myudp.h" //udp header //#include "QDeadlineTimer" addUser::addUser(QWidget *parent) : QDialog(parent), ui(new Ui::addUser) { ui->setupUi(this); } addUser::~addUser() { delete ui; } void addUser::on_pushButton_2_clicked() //submit button press { addUser User; QMessageBox mssgBox; addUserName = ui->lineEdit->text(); addNewID = ui->lineEdit_2->text(); addRFID = ui->lineEdit_3->text(); IP = ui->lineEdit_4->text(); port = ui->lineEdit_5->text(); portNum = port.toInt(); int rfidLength = addRFID.length(); int lengthOfName = addUserName.length(); strLength = QString::number(lengthOfName); // number to string if((rfidLength == 10) && (lengthOfName <11)) { mssgBox.setWindowTitle("Confirmation"); mssgBox.setText("Confirm the new user details" ); mssgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Save); int returnValue = mssgBox.exec(); switch(returnValue) { case QMessageBox::Save : { //packetize and send the details over UDP packetToDevice = "FA"+addNewID+strLength+addUserName+addRFID; myudp.udpSend(packetToDevice, IP, portNum); //UDP SEND Fn CALL User.show(); // back to the window break; } case QMessageBox::Cancel : User.show(); //back to window break; default: qDebug()<<"entered default state"; } } else { QMessageBox falseMssg; falseMssg.setWindowTitle("Error"); falseMssg.setText("Invalid RFID/Name"); falseMssg.setStandardButtons(QMessageBox::Close); falseMssg.exec(); User.show(); } }
the above one is the file where myudp object is to be used to send and readyRead should be emitted whenever datagram arrives.
then the udp header and source is as follows:
//myUdp.h #ifndef MYUDP_H #define MYUDP_H #include <QObject> #include <QUdpSocket> class myUDP : public QObject { Q_OBJECT public: explicit myUDP(QObject *parent = nullptr); void udpSend(QString, QString, int); signals: public slots: void receiveUDP(); private: QUdpSocket *socket2; }; #endif // MYUDP_H
//myudp.cpp #include "myudp.h" #include "QUdpSocket" #include "QDebug" #include "adduser.h" // myUDP::myUDP(QObject *parent) : QObject{parent} { socket2 = new QUdpSocket(this); bool result = socket2->bind(QHostAddress::Any, 1234); qDebug()<<"bind status"<<result; connect(socket2, SIGNAL(readyRead()), this, SLOT(receiveUDP())); void myUDP::udpSend(QString a, QString ip, int port) { const char *str; QByteArray dataToSend; dataToSend = a.toLatin1(); //Qstring to const char * str = dataToSend.data(); int retVal = socket2 ->writeDatagram(str, QHostAddress(ip), port); qDebug()<< retVal; // length of the transmitted string if successfully transmitted or -1 } void myUDP::receiveUDP() //called when readyRead SIGNAL is emitted. { while (socket2->hasPendingDatagrams()) { QByteArray datagram; datagram.resize(socket2->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; socket2->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort ); qDebug()<<datagram; qDebug()<<"IP "<<sender.toString(); } }
-
I don't understand what you're doing - why do you try to send something to yourself? I doubt you receive data on the same socket you're currently writing it.
-
@febinaf I'm reasonably sure, that
writeDatagram
void myUDP::udpSend(QString a, QString ip, int port) { const char *str; QByteArray dataToSend; dataToSend = a.toLatin1(); //Qstring to const char * str = dataToSend.data(); int retVal = socket2 ->writeDatagram(str, QHostAddress(ip), port); qDebug()<< retVal; // length of the transmitted string if successfully transmitted or -1 }
is an asynchronous process. So there might be some lifetime issues...
-
@Christian-Ehrlicher I'm writing data to IP and port entered using user input. And I've used the same socket to send and receive, as its being shown in online tutorials ..... https://www.youtube.com/watch?v=4qx4FaglSig
I've checked the above code in another project with just main function and udp class, and both reading and writing works fine using a single socket.