[Solved] QTcpSocket : readyRead() never emetted



  • Hello,
    First, I thank the Qt team for offering us a so great tool !!!

    My problem is that I've got a QTcpSocket object which never emit readyRead().
    Here is the code :

    client.h

    #define CLIENT_H
    
    #include <QObject>
    #include <QString>
    #include <QTcpSocket>
    #include <QMessageBox>
    #include <QApplication>
    #include <QWidget>
    #include "demandeipport.h"
    
    class Client : public QObject
    {
        Q_OBJECT
    public:
        explicit Client(QObject *parent = 0);
        int sendMessage(QString message);
        QString message() const;
        int etat() const;
    private:
        QTcpSocket * m_socket;
        QString m_message;
        DemandeIpPort * m_fenetreDemande;
        int m_etat;
    signals:
        void bienConnecte();
    public slots:
        void readyRead();
        void connexion();
    };
    
    #endif // CLIENT_H
    

    And part of client.cpp

    #include "client.h"
    
    Client::Client(QObject *parent) : QObject(parent), m_message(""), m_etat(0)
    {
        m_fenetreDemande = new DemandeIpPort;
        connect(m_fenetreDemande, SIGNAL(demandeConnexion()), this, SLOT(connexion()));
        m_fenetreDemande->show();
    }
    
    void Client::connexion() {
        // connexion au serveur
        m_socket = new QTcpSocket(this);
    
        qDebug() << "Connexion...";
        //connexion
        m_socket->connectToHost(m_fenetreDemande->ip(), m_fenetreDemande->port());
    
        // attend la connextion
        if(! m_socket->waitForConnected()) {
            // Affichage du message d'errur
            QMessageBox erreurMessageBox;
            switch (m_socket->error()) {
                case QAbstractSocket::HostNotFoundError:
                    erreurMessageBox.setText(tr("Erreur : le serveur n'a pas pu être trouvé."));
                    erreurMessageBox.setInformativeText(tr("Vérifiez l'adresse IP et le port."));
                break;
            case QAbstractSocket::ConnectionRefusedError:
                erreurMessageBox.setText(tr("Erreur : le serveur a refusé la connexion."));
                erreurMessageBox.setInformativeText(tr("Vérifiez si le serveur a bien été lancé. Vérifiez aussi l'adresse IP et le port."));
                break;
            case QAbstractSocket::RemoteHostClosedError:
                erreurMessageBox.setText(tr("Erreur : le serveur a coupé la connexion."));
                erreurMessageBox.setInformativeText(tr("Veuillez réessayer plus tard."));
                break;
            default:
                erreurMessageBox.setText(tr("Erreur : ") + m_socket->errorString());
                break;
            }
            erreurMessageBox.setIcon(QMessageBox::Critical);
            erreurMessageBox.setWindowTitle(tr("Erreur - Connexion au serveur"));
            erreurMessageBox.setStandardButtons(QMessageBox::Close);
            if (erreurMessageBox.exec() == QMessageBox::Close) {
                qApp->quit();
            }
         }
        else {
            qDebug() << "Client : bien connecté !";
            emit bienConnecte();
        }
    }
    
    int Client::sendMessage(QString message)
    {
        if(! m_socket->isValid())
            return -1;
        qDebug() << "Client : Envoi du message au serveur";
        QByteArray Buffer;
        Buffer.append(message);
    
        if(m_socket->write(Buffer) == -1) {
            return -1;
        }
        qDebug() << "Client : message apparement bien envoyé";
        if(m_socket->flush())
            return 0;
        else
            return -1;
    }
    
    void Client::readyRead()
    {
        qDebug() << "Client : canReadLine()";
        while(! m_socket->canReadLine()) ;
        m_message = QString::fromUtf8(m_socket->readLine());
    }
    
    

    Could you help me please ?
    Thank you in advance !



  • I found a solution : I used the waitForReadyRead() function.
    I still don't know why my code didn't work.

    Edit : I found that I forgot to connect the slot with the signal, but even with that, it didn't work...