ReadyRead() signal is not being emitted (server side)



  • I have created a server part, and everything works ok the data is being received from the client but I can get it only If I press the button which will read the data from the socket is there any way to signal that socket is filled with data and is ready to read. I have found that I can use the ReadyRead signal but it's not being emitted when the socket has the data in it, what can be the problem and how I can solve it?
    Server side code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        socket = new QTcpSocket(this);
        connect(&server,SIGNAL(newConnection()),this,SLOT(newCon()));
        connect(socket,SIGNAL(readyRead()),this,SLOT(on_pushButton_clicked()));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    void MainWindow::ListenS(){
        qDebug()<<"Listening";
        //socket->bind(QHostAddress::LocalHost,5555,QAbstractSocket::DontShareAddress);
        if(server.listen(QHostAddress::LocalHost,5555)){
        }
    }
    void MainWindow::newCon(){
        ui->label->setText("Connected");
        socket->open(QIODevice::ReadWrite);
        socket = server.nextPendingConnection();
        socket->waitForReadyRead(500);
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        QByteArray ba;
        ui->label->setText(QString::number(socket->bytesAvailable()));
        if(socket->bytesAvailable() <=0){
            mes = "Error";
        }else{
        ba = socket->readAll();
        QString g(ba);
        mes = g;
        }
        QImage img = QImage::fromData(ba);
        ui->label->setPixmap(QPixmap::fromImage(img));
    }
    

  • Moderators

    @mandruk1331 You're not using the socket you connected:

    socket = new QTcpSocket(this);
    connect(&server,SIGNAL(newConnection()),this,SLOT(newCon()));
    connect(socket,SIGNAL(readyRead()),this,SLOT(on_pushButton_clicked()));
    

    now

    socket = server.nextPendingConnection();
    

    you need to connect the signal from the socket you get from server.nextPendingConnection();
    For each new connection you get a new socket.
    That's why you do not need the socket you create in the constructor. Just do:

    socket = server.nextPendingConnection();
    connect(socket,SIGNAL(readyRead()),this,SLOT(on_pushButton_clicked()));
    


  • Hi, I'm not 100% sure, but I think your readyRead Signal is not emitted, because you call WaitForReadyRead, right after connecting, and after that, no now data arrives at the socket -> no Signal later.

    @mandruk1331 said in ReadyRead() signal is not being emitted (server side):

    void MainWindow::newCon(){
    ui->label->setText("Connected");
    socket->open(QIODevice::ReadWrite);
    socket = server.nextPendingConnection();
    socket->waitForReadyRead(500);
    }


  • Moderators

    @mandruk1331

    Checkout the server example found here.

    @mandruk1331 said in ReadyRead() signal is not being emitted (server side):

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        socket = new QTcpSocket(this);
        connect(&server,SIGNAL(newConnection()),this,SLOT(newCon()));
        connect(socket,SIGNAL(readyRead()),this,SLOT(on_pushButton_clicked()));
    }
    

    You connect your signal with a socket pointer above.

    @mandruk1331 said in ReadyRead() signal is not being emitted (server side):

        void MainWindow::newCon(){
        ui->label->setText("Connected");
        socket->open(QIODevice::ReadWrite);
        socket = server.nextPendingConnection();
        socket->waitForReadyRead(500);
    }
    

    In this routine you are writting another pointer and basically disconnect this signal. Place the connect in routine newCon() after assigning the pointer.



  • @jsulm this solution helped me. Thank you. Now I can continue working on my project.
    And one more thing how I can send a response from the server?


Log in to reply
 

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