Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. ReadyRead() signal is not being emitted (server side)
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 4.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mandruk1331M Offline
    mandruk1331M Offline
    mandruk1331
    wrote on last edited by
    #1

    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));
    }
    

    Mandruk1331

    jsulmJ J.HilkJ K 3 Replies Last reply
    0
    • mandruk1331M mandruk1331

      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));
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      mandruk1331M 1 Reply Last reply
      3
      • mandruk1331M mandruk1331

        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));
        }
        
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        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);
        }


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        0
        • mandruk1331M mandruk1331

          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));
          }
          
          K Offline
          K Offline
          koahnig
          wrote on last edited by koahnig
          #4

          @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.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • jsulmJ jsulm

            @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()));
            
            mandruk1331M Offline
            mandruk1331M Offline
            mandruk1331
            wrote on last edited by mandruk1331
            #5

            @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?

            Mandruk1331

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved