readyRead() slot not called for second received message
-
Hi,
I am writing a small TCP Client Qt Console Application that receives messages from a server. The client connects OK to the server. The server then sends a welcome message which the clients receives via my readyRead slot. But when the server sends another message, say 15 seconds later, the readyRead slot is not called.
The code looks like this:
myclient.h
#ifndef MYCLIENT_H #define MYCLIENT_H #include <QObject> #include <QTcpSocket> #include <QDebug> class MyClient : public QObject { Q_OBJECT public: MyClient(QObject *parent = 0); void Connect(quint16 servicePort); private: QTcpSocket *socket; QByteArray dataIn; public slots: void readyRead(); void disconnected(); }; #endif // MYCLIENT_H
myclient.cpp
#include <time.h> #include "myclient.h" MyClient::MyClient(QObject *parent) : QObject(parent) { } void MyClient::Connect(quint16 servicePort) { socket = new QTcpSocket(this); connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection); connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()), Qt::DirectConnection); // Connect socket->connectToHost("localhost",servicePort); if(socket->waitForConnected(3000)) qDebug() << "Connected to " << servicePort; else qDebug() << "Unable to connect to " << servicePort; } void MyClient::readyRead() { dataIn = this->socket->readAll(); qDebug() << "dataIn: " << dataIn; } void MyClient::disconnected() { qDebug() << "disconnected"; this->socket->deleteLater(); exit(0); }
main.cpp
#include <QCoreApplication> #include "myclient.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); MyClient client1; client1.Connect(8090); return a.exec(); }
After the readyRead is called the first time, do I need to do something to cause the socket to listen again for incoming data, and so call the readyRead slot again?
Environment is Windows 7 and Qt 5.5.1.
Thanks and regards...Paul
-
Hi
It all look fine as far as I can see.
Could you try with
Qt::QueuedConnection
instead ? -
It's suddenly started to work correctly - I have no idea why. I'll leave this open a little longer in case it stops working again.
-
@PaulOfford
Ok. good news.
Well maybe u did a full rebuild or something.