Qt QUdpSocket: readyRead() signal and corresponding slot not working as supposed - c++

I have problems to find why my short QUdpSocket example is not working. I plan to use only one UDP socket to read and write to an embedded device at 192.168.2.66 on port 2007. The device will reply always on port 2007 to the sender. I tested the device with an UDP terminal software and works as I said. So, I designed a simple class to embed the functions needed to manage the device:
class QUdp : public QObject
{
// Q_OBJECT
public:
explicit QUdp(QObject *parent = 0, const char *szHost = 0, uint16_t wPort = 0);
~QUdp();
bool Open();
int64_t Write(QByteArray &data);
int64_t Write(QString strData);
private:
QString m_strHost;
uint16_t m_wPort;
QUdpSocket *OUdp;
private slots:
void received();
};
I suppose that the problem is in the Open method:
bool QUdp::Open()
{
QHostAddress OHost;
connect(OUdp, &QUdpSocket::readyRead, this, &QUdp::received);
bool zRet = OUdp->bind(QHostAddress::AnyIPv4, m_wPort, QUdpSocket::ShareAddress);
OHost.setAddress(m_strHost);
OUdp->connectToHost(OHost, m_wPort, QIODevice::ReadWrite);
return(zRet);
}
//------------------------------------------------------------------------
I used the Qt 5 syntax for the connect(), m_strHost value is "192.168.2.66" and m_wPort is 2007
my Write method is very simple (the part inside #if 0 was added to see if the socket received any data)
int64_t QUdp::Write(QString strData)
{
QByteArray data(strData.toStdString().c_str(), strData.length());
int64_t iCount = OUdp->write(data);
#if 0
bool zRecved = OUdp->waitForReadyRead(3000);
int64_t iRecvCount = OUdp->bytesAvailable();
#endif
return(iCount);
}
//------------------------------------------------------------------------
and this is my test received() method... I wrote it just to see if the signal-slot works or not:
void QUdp::received()
{
int64_t iRecvCount = OUdp->bytesAvailable();
}
//------------------------------------------------------------------------
I don't understand what is wrong.. I found some posts saying that is not possible read and write using only one UDP socket in Qt (Qt uses BSD sockets so it should be possible) but my example looks as the proposed solutions so I really don't understand what is not working.

You can read and write using just one UDP socket in Qt. I have this running in Qt5 on both windows and Linux, so no worries there :)
To establish Rx direct comms in QUdpSocket you should really use the bind() function, something like this:
// Rx connection: check we are not already bound
if (udpSocket->state() != udpSocket->BoundState)
{
// Rx not in bound state, attempt to bind
udpSocket->bind(address, port);
}
Once this has completed you will be able to check that udpSocket->state() == udpSocket->BoundState is true, then you are successfully "bound" to this ip/port. Now your listening can begin if your connection to readready() is correct. I have not used this connection syntax that you are using, so I can't say much about that, but here is the example of how I connect:
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(rxDataEvent()), Qt::QueuedConnection);
Where "this" is the class which contains my QUdpSocket and udpSocket is a QUdpSocket pointer. Then rxDataEvent is defined below:
void CIpComms::rxDataEvent(void)
{
QByteArray rxData;
QHostAddress sender;
quint16 senderPort;
while (udpSocket->hasPendingDatagrams())
{
// Resize and zero byte buffer so we can make way for the new data.
rxData.fill(0, udpSocket->pendingDatagramSize());
// Read data from the UDP buffer.
udpSocket->readDatagram(rxData.data(),
rxData.size(),
&sender,
&senderPort);
// Emit ipDataReceived Signal
emit ipDataReceived(rxData);
}
}
Here we continually check for datagrams until there are none pending (bit easier then doing the whole "bytesAvailable thing") and stick the data into a QByteArray and emit it off elsewhere (which you obviously don't have to do!).
That is all you need to do for connection. Then to send is very easy, you simply have to call writeDatagram(), well there are other options but this is by far the easier to use:
if (-1 == udpSocket->writeDatagram(txData, address, port))
{
// Data write failed, print out warning
qWarning() << "Unable to write data to " << address.toString() << ":" << port << endl;
return false;
}
I have pretty much cut and pasted this from my working code (with a few edits to keep it short-n-simple so it should give you a starting point. In summary where I believe you are going wrong is that you have not "bound" to the IP address/port and are therefore NOT listening to it and will not receive any readReady() events.

Related

QUdpSocket loses validity

I've a question regarding QUdpSocket:
I'm receiving UDP messages on port 50011 to multicast address 239.0.0.1 about every 0.2 seconds (Proven by wireshark).
The following code is working well (and printing "VALID" on every message) for ~ 1 minute. After that messageHandler is not being called by the QUdpSocket's signal readyRead anymore (even though I've double checked with wireshark, and messages are still being send).
ServiceDiscovery::ServiceDiscovery(QObject *parent) :
QObject(parent),
socket(new QUdpSocket(this))
{
socket->bind(QHostAddress::AnyIPv4, 50011, QUdpSocket::ShareAddress);
socket->joinMulticastGroup(QHostAddress("239.0.0.1"));
connect(socket, &QUdpSocket::readyRead,
this, &ServiceDiscovery::messageHandler,
Qt::DirectConnection);
}
void ServiceDiscovery::messageHandler()
{
if(socket->isOpen()) qDebug("OPEN"); // Does not print, ofc.
if(socket->isReadable()) qDebug("READABLE"); // Does not print.
if(socket->isValid()) qDebug("VALID"); // Suddenly stops.
QByteArray datagram;
while(socket->hasPendingDatagrams()) {
datagram.resize(socket->pendingDatagramSize());
socket->readDatagram(datagram.data(), datagram.size());
Message response(datagram);
if(response.deserialize()) {
if(response.getServiceID() == constants::servicediscovery::SERVICE_ID) {
QByteArray payload = response.getPayload();
if(payload.size() >= 48) {
QString address = QHostAddress(payload.mid(32, 4).toHex().toUInt(nullptr, 16)).toString();
quint16 port = payload.mid(38, 2).toHex().toUInt(nullptr, 16);
emit found(address, port);
}
}
}
}
}
What am I doing wrong here ?
Thanks in advance.
EDIT: Due to comments I pasted the datagram handling, too.
For the Records:
I had the same problem. The reason: A demo library my Application is relying on closes after the evaluation time not only its open ports but all ports of the application.

wait for data arrive from serial port in Qt

I use serial port connection in my qt application.
My problem- I cant get back the control (or the values comes from the comm port) after sending the command.
I have a class named serial.cpp which responsible to serial port connection.
This class contains 2 queues. one for save bytes from the comm port and second for the decoded messages. the class has the functions below:
void Serial::sendCommand(QString s)
{
QString sometext = s;
QByteArray ba = QByteArray::fromHex(sometext.toLatin1());
serial->write(ba);
}
void Serial::serialReceived()
{
QByteArray ba ;
serialArray = serial->readAll();
for (int i=0; i<sizeof(serialArray);i++)
{
queueBytes.enqueue(serialArray[i]); // queue for saving the bytes
}
QVector<int> vect = queueBytes.toVector();
packetSize = 6;
if (vect.size() >= packetSize)
{ // the whole packet arrived
for (int i =0 ;i<packetSize;i++)
{
item = queueBytes.dequeue();
ba.append(item);
}
}
if (ba.toHex() == "12ee02010176")
queueMsgs.enqueue("ACK");
// ... and so on
}
here is the call class:
void Engine::onNewMessageFromAppReceived(int msgId,QString args)
{
serial->sendCommand("ee1203190209005569");
while (serial->queueMsgs.size() == 0) // infinite loop-the queue is always empty
{
usleep(1);
}
QVector<QString> vect2 = serial->queueMsgs.toVector();
qDebug() << vect2 << "get ack---" ;
}
please your help
The QSerialPort class inherits from QIODevice which has waitFor... methods that may be what you're looking for. Take a look at these docs.
If you want to handle the serial port asynchronously, use the readyRead signal and perform reading in a function that you connected to that signal. If you don't mind that the operation is blocking, the waitForReadyRead function is what you're looking for.
The good way
Here's the proper way to do it using Qt's signals and slots mechanism. This will not block your GUI and lets your application respond to user actions even while you are waiting for the serial port.
Connect a function to the bytesWritten signal
The code you want to execute after you sent data through the serial port should be placed in this function.
Connect a function to the readyRead signal
The code you want to execute after you read some data from the serial port should be placed in this function.
Open the port
The bad way
In some cases you can do it like this, but it's blocking, meaning that your GUI will freeze while your app is waiting for the serial port. I don't recommend doing it like this.
Open the port
Send data
Call waitForBytesWritten
Call waitForReadyRead
Working example code
Qt has a vast amount of working example code. There are even examples about how to use QSerialPort, and they are well worth checking out. You might be most interested in the async writer example and the async reader example.

Qt C++ Console Server, Wait for socket connection & accept input at same time?

I am writing a server as a Qt console application. I have the server set up to wait for a socket connection, but I also need to allow a user to input commands into the server for managing it. Both are working independently. However, the problem I ran into is that when I'm in a while loop accepting and processing input commands, the server doesn't accept connections.
I have a Socket class, and in its constructor, I have:
connect(server,SIGNAL(newConnection()),this, SLOT(newConnection()));
Right under that in the constructor, I call a function that has a more in-depth version of this for getting commands from the user:
QTextStream qin(stdin, QIODevice::ReadOnly);
QString usrCmd;
while(usrCmd != "exit" && usrCmd != "EXIT") {
//Get command input and process here
}
Inside newConnection(), I just accept the next connection and then use the socket.
QTcpSocket *serverSocket = server->nextPendingConnection();
How can I make it so the socket can wait for connections and wait for user-inputed commands at the same time?
Problem with your code is because you are blocking event loop with your while loop. So, the solution to your problem is to read from stdin asynchronously. On Linux (and on Mac, I guess), you can use QSocketNotifier to notify when the data is arrived on stdin, and to read it manually), as per various internet sources.
As I am using Windows, I would suggest you to do it in this way (which should work on all platforms):
Open the thread for reading data from stdin
Once you get some data (perhaps line?) you can use Qt signal-slot mechanism to pass the data to main thread for processing without blocking the event loop.
So, this is the pseudocode. MainAppClass should your existing server class, just edit the constructor to create new thread, and add new slot for processing the data.
class Reader: public QThread
{
Q_OBJECT
public:
Reader(QObject * parent = 0 ): QThread(parent){}
void run(void)
{
forever{
std::string data;
std::getline (std::cin, data);
if(data == "exit")
{
emit exitServer();
return;
}
emit dataReady(QString::fromStdString(data));
}
}
signals:
void dataReady(QString data);
void exitServer();
};
class MainAppClass: public QObject
{
Q_OBJECT
public:
MainAppClass()
{
Reader * tr = new Reader(this);
connect(tr, SIGNAL(dataReady(QString)), this, SLOT(processData(QString)));
connect(tr, SIGNAL(exitServer()), this, SLOT(exitServer()));
tr->start();
}
public slots:
void processData(QString data)
{
std::cout << "Command: " << data.toStdString() << std::endl;
}
void exitServer()
{
std::cout << "Exiting..." << std::endl;
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainAppClass myapp; //your server
app.exec();
return 0;
}
Since I wrote simple guidelines how to use QTcpSocket, here is the brief
When you get client QTcpSocket, connect readyRead() signal to some slot, and read data from sender() object. You don't need to read anything in the constructor.
For reading you can use standard QIODevice functions.
Note: this is pseudo code, and you may need to change few things (check the state of the stream on reading, save pointer to sockets in some list, subscribe to disconnected() signal, call listen() in constructor, check if QTcpServer is listening, etc).
So, you need to have slot onReadyRead() in your class which will have the following code:
void Server::readyReadSlot()
{
QTcpSocket *client = (QTcpSocket*)sender(); // get socket which emited the signal
while(client->canReadLine()) // read all lines!
// If there is not any lines received (you may not always receive
// whole line as TCP is stream based protocol),
// you will not leave data in the buffer for later processing.
{
QString line = client->readLine();
processLine(line); // or emit new signal if you like
}
}
Inside newConnection() you need to connect readyRead() signal with your slot.
void Server::newConnection()
{
QTcpSocket *clientSocket = server->nextPendingConnection();
connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readyReadSlot()));
}

Unable to read correctly from socket

I have a server application which sends some xor encrypted strings. I am reading them from my QT client application. Sometimes, the server is slower and I am not able to receive the entire string. I have tried something like below but it gets stuck ( see the comment below). How can I wait until I have the entire data. I tried bytesAviable() but then again i get stuck (infinite loop)
QTcpSocket * sock = static_cast<QTcpSocket*>(this->sender());
if (key == 0)
{
QString recv(sock->readLine());
key = recv.toInt();
qDebug() << "Cheia este " << key;
char * response = enc_dec("#AUTH|admin|admin",strlen("#AUTH|admin|admin"),key);
sock->write(response);
}
else
{
busy = true;
while (sock->bytesAvailable() > 0)
{
unsigned short word;
sock->read((char*)(&word),2);
qDebug()<<word;
//Sleep(100); if i do this than it works great!
QByteArray bts = sock->read(word);
while (bts.length() < word)
{
char bit; //here get's stuck
if (sock->read(&bit,1) > 0)
bts.append(bit);
sock->flush();
}
char * decodat = enc_dec((char*)bts.data(),bts.length() - 2,key);
qDebug() << decodat;
}
}
I don't know what the meaning of key == 0 is, but you are almost certainly misusing available(), like almost everybody else who has ever called it, including me. It tells you how much data can be read without blocking. It has nothing to do with how much data may eventually be delivered down the connection, and the reason is that there are TCP APIs that can tell you the former, but not the latter. Indeed the latter doesn't have any real meaning, considering that the peer could keep writing from now until Doomsday. You should just block and loop until you have read the amount of data you need for the next piece of work.
I offer you to do the following:
QObject::connect(this->m_TCPSocket, SIGNAL(readyRead()), this, SLOT(processRecivedDatagrams()));
Some explanation:
It is convinient to create a class instance of which will manage network;
One has the member which is pointer on TCPSocket;
In constructor implement connection of signal from socket readyRead() which is emmited when needed data was delivered with SLOT(processRecivedDatagrams()). which is responsible for processing recived datagrams/ in this case it is processRecivedDatagrams(), also implement this slot
Mind that class which manages network has to inherit from QObject and also in its declaration include macrosQ_OBject` for MOC.
update:
i also offer you to store recived data in container like stack or queue this will allow you to synhronize sender and reciver (container in this case acts like buffer)
// SLOT:
void Network::processRecivedDatagrams(void)
{
if (!this->m_flagLocked) // use analog of mutex
{
this->m_flagLocked = true; // lock resource
QByteArray datagram;
do
{
datagram.resize(m_TCPSocket->pendingDatagramSize());
m_TCPSocket->readDatagram(datagram.data(), datagram.size());
}
Qt::String YourString; // actualy I don`t remember how to declare Qt string
while (m_TCPSocket->hasPendingDatagrams());
QDataStream in (&datagram, QIODevice::ReadOnly);
in >> YourString
--numberOfDatagrams;
}
this->m_flagLocked = false; // unlock resource
}
}

Problem between a QTcpServer and a TCPClientSocket (Cayuga)

I try to communicate via TCP Socket between a QT4-Application (MyApp) and Cayuga (written in C++).
The connection part works fine, i.e. Cayuga connects to MyApp.
Now, MyApp is sending some data to Cayuga, but nothing is received.
void MyApp::init()
QTcpServer *m_server;
QTcpSocket *clientConnection;
//Open socket for transmission
m_server = new QTcpServer(this);
if (!m_server->listen(QHostAddress::Any, m_port)) {
//Error handling
return;
}
connect(m_server, SIGNAL(newConnection()), this, SLOT(startSend()));
void MyApp::startSend()
{
clientConnection = m_server->nextPendingConnection();
}
The writting is done here:
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << s;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
clientConnection->write(block);
clientConnection->flush();
My tutor suggested to use an external library (cudb) if I cannot get it to work with QTcpSockets. That does not feel right and that's why I hope you have a better answer to my problem.
This is my guess of what's happening:
QDataStream implements a serializing protocol (Hence having to specify a version (Qt_4_0) for it). You need something on the other end that understands that protocol (to wit, another Qt_4_0 DataStream). Particularly, QDataStream makes sure you get the right data regardless of the endianness of the sending and receiving ends.
Instead of serializing to a block and then writing the block, you can try something like:
QDataStream out(clientConnection, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out.writeRawData(data, length);
clienConnection->flush();
writeRawData() does not marshall your data...