QTcpClient - readReady() never gets emitted
-
Hello!
I am trying to write a simple TCP Client to communicate with my node.js server. I am a beginner at doing networking with Qt. I have set up the following class for the client:
Header:
@#ifndef TCPCLIENT_H
#define TCPCLIENT_H#include <QObject>
#include <QTcpSocket>
#include <QThread>class TCPClient : public QThread
{
Q_OBJECT
public:
explicit TCPClient(QString hostname = "", int port = 0);
void write(QByteArray data);private:
QTcpSocket *socket;
quint16 blockSize;private slots:
void error(QAbstractSocket::SocketError error);
void read();
};#endif // TCPCLIENT_H
@Cpp:
@#include "tcpclient.h"TCPClient::TCPClient(QString hostname, int port)
{
socket = new QTcpSocket(this);connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError))); connect(socket, SIGNAL(readyRead()), this, SLOT(read())); socket->connectToHost(hostname, port); socket->waitForConnected();
}
void TCPClient::error(QAbstractSocket::SocketError error){
qDebug() << error;
}void TCPClient::read(){
qDebug() << "asd";
}void TCPClient::write(QByteArray data){
socket->write(data);
socket->flush();
}
@I can connect to the server, and send data to it. The problem is that the readyRead signal seems to get never emitted. When I connect to the server it sends automatically a message, but I never receive this with this client. Also, I send some data in another class to the server, the server even processes it, but the client never gets the response.
Here is the output of my server:
@[IP ADDRESS] - connected
[IP ADDRESS] - getnews request, response: OK
[IP ADDRESS] - ECONNRESET error
[IP ADDRESS] - connection closed@As you can see, it handles client connect, disconnect and messages. There is no problem with the server, I tested it during development with simple telnet clients.
Also, if I write this code to the end of the consturctor, I get the welcome message perfectly, but the readyRead signal is not getting emitted.
@socket->waitForReadyRead();
qDebug() << socket->readAll()@Does anybody know how to fix this issue?
-
Do you have an event loop running?
Also there is the "fortuneClient example.":http://doc.qt.io/qt-5/qtnetwork-fortuneclient-example.html With the dialog is has more than you need, but it may be a good start for you.