TcpSocket signal readyRead not received



  • I'm having the issue mentioned inside the subject when building an tcp client for a server that is created with socat. Is there someone that had the same issue ?

    #redirect the stdio on tcp port 2728
    socat - tcp-listen:2728,reuseaddr,fork

    On the same machine, I'm running the code that following(more or less, because I'm changing the code at each 5 minutes in case I have a tip):

    CSocatMonitor::CSocatMonitor(QObject *parent)
        : QDialog(parent)
    {
        qDebug() << "The fun is starting...";
    }
    void CSocatMonitor::init()
    {
        socket =  new QTcpSocket(this);
    
        in.setDevice(socket);
        in.setVersion(QDataStream::Qt_5_9);
    
        connect(socket, SIGNAL(readyRead()),this, SLOT(onReadyRead()));
        connect(socket, &QAbstractSocket::connected,this, &CSocatMonitor::connected);
        connect(socket, &QAbstractSocket::disconnected,this, &CSocatMonitor::disconnected);
        connect(socket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
                     this, &CSocatMonitor::getError);
    
        socket->connectToHost("127.0.0.1",2728);
    
    }
    void CSocatMonitor::onReadyRead()
    {
        in.startTransaction();
    
        QString nextFortune;
        in >> nextFortune;
    
        if (!in.commitTransaction())
            return;
    
        QMessageBox::information(this, tr("Fortune Client"),
                                 tr("The following data occurred: %1.")
                                 .arg(nextFortune));
        socket->flush();
    
    }
    
    void CSocatMonitor::connected()
    {
        qDebug() << "connected...";
        QMessageBox::information(this, tr("connected..."),
                                 tr("connected..."
                                    "connected..."));
        QMessageBox::information(this, tr("Fortune Client"),
                                 tr("The following data(remaining length %1) occurred: %2.")
                                 .arg(QString(socket->readAll()))
                                 .arg(socket->readAll().length()));
        socket->flush();
    }
    
    void CSocatMonitor::disconnected()
    {
        QMessageBox::information(this, tr("DISconnected..."),
                                 tr("DISconnected..."
                                    "DISconnected..."));
    }
    void CSocatMonitor::getError(QAbstractSocket::SocketError socketError)
    {
         qDebug() << "Socket error:" << socketError;
         QMessageBox::information(this, tr("Fortune Client"),
                                  tr("The following error occurred: %1.")
                                  .arg(socket->errorString()));
    }
    


  • Did you declare onReadyRead as slot?
    Otherwise, to be on the safe side, change connect(socket, SIGNAL(readyRead()),this, SLOT(onReadyRead())); to connect(socket, &QTcpSocket::readyRead,this, &CSocatMonitor::onReadyRead);



  • @VRonin ,onReadyRead is a part of public slots.
    I tried that you suggested and also

    connect(socket, &QIODevice::readyRead,this, &CSocatMonitor::onReadyRead);
    

    The result is the same.



  • Are you blocking the event loop?



  • @VRonin I'm not blocking anything(AFIK)...The connect is triggered, also, the errors...



  • How are you sending the data to this client?



  • @VRonin Just typing into the stdio. This output is provided by socat over the TCP.



  • @Catalin.O said in TcpSocket signal readyRead not received:

    Just typing into the stdio. This output is provided by socat over the TCP.

    Can you post code?



  • @Taz742 You mean the headers and all the sources ?



  • @Catalin.O
    Yes, it would be better.


  • Qt Champions 2016

    @Catalin.O said in TcpSocket signal readyRead not received:

    in.startTransaction();

    QString nextFortune;
    in >> nextFortune;
    
    if (!in.commitTransaction())
        return;
    

    Unless your server knows what Qt is and how it should send you QStrings through QDataStream version 5.9, then this should always fail and you'd get nowhere with your slot.



  • @Taz742 Ok....


    class CSocatMonitor : public QDialog
    .....
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    CSocatMonitor * monitor = CSocatMonitor::getInstance();
    monitor->init();

    return a.exec();
    

    }



  • @kshegunov Your suggestion solved my blocking point. Thank you.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.