Important: Please read the Qt Code of Conduct -

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:

    @#ifndef TCPCLIENT_H
    #define TCPCLIENT_H

    #include <QObject>
    #include <QTcpSocket>
    #include <QThread>

    class TCPClient : public QThread
    explicit TCPClient(QString hostname = "", int port = 0);
    void write(QByteArray data);

    QTcpSocket *socket;
    quint16 blockSize;

    private slots:
    void error(QAbstractSocket::SocketError error);
    void read();

    #endif // TCPCLIENT_H

    @#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);


    void TCPClient::error(QAbstractSocket::SocketError error){
    qDebug() << error;

    void TCPClient::read(){
    qDebug() << "asd";

    void TCPClient::write(QByteArray data){

    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] - 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.
    qDebug() << socket->readAll()@

    Does anybody know how to fix this issue?

  • Do you have an event loop running?

    Also there is the "fortuneClient example.": With the dialog is has more than you need, but it may be a good start for you.

Log in to reply