Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTcpServer readyRead() Signal emited twice
Qt 6.12 LTS is out! See what's new in the release blog

QTcpServer readyRead() Signal emited twice

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 6 Posters 7.2k Views 2 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.
  • Taz742T Offline
    Taz742T Offline
    Taz742
    wrote on last edited by Taz742
    #1

    I have QTcpServer. I want to send large data from client side and how to catch signal, when all data is received on server?
    "while (socket->bytesavailable)" doesn't work.

    For example:

    when qbytearray size is 9000, which is send from client, on the server it's 4000 or 5000...

    Example Two:

    In this Case readyRead() SIGNAL Is Emited 8 times.

        void Client::SendMessage(std::vector<QString>)
        {
            MyClass _Send;
                    _Send.Age = 22;
                    _Send.School = 14;
                    _Send.Name = "Taz";
    
            QVector<MyClass2> vv;
    
            for (int i = 0; i < 15000; i++) {
                vv.push_back(MyClass2(24, "leri"));
                vv.push_back(MyClass2(22, "tazo"));
            }
    
            _Send.vctr = vv;
    
            QByteArray bytes;
            QDataStream stream(&bytes, QIODevice::WriteOnly);
    
            int FunctionUID = 331;
            int ij, ik = ij = 169;
            MyClass2 faf(-31, "15");
    
            stream << FunctionUID << _Send << faf << ij << ik;
    
            socket->write(bytes);
        }
    
    
    
        void Server::ImReady()
        {
            QByteArray buf;
    
            buf = socket->readAll();
    
            QDataStream stream(&buf, QIODevice::ReadOnly);
    
            int FunctionUID, ij, ik;
            MyClass vv;
            MyClass2 vv1;
    
            stream >> FunctionUID >> vv >> vv1 >> ij >> ik;
    
            qDebug() << vv.vctr.size() << "faf";
        }
    
        void Server::incomingConnection(qintptr val)
        {
            qDebug() << "Client Connected" << val;
    
            socket = new QTcpSocket;
            socket->setSocketDescriptor(val);
    
            if (!socket) {
                return;
            }
    
            connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
            connect(socket, SIGNAL(readyRead()), this, SLOT(ImReady()));
        }
    

    Do what you want.

    K Taz742T 2 Replies Last reply
    0
    • Taz742T Taz742

      I have QTcpServer. I want to send large data from client side and how to catch signal, when all data is received on server?
      "while (socket->bytesavailable)" doesn't work.

      For example:

      when qbytearray size is 9000, which is send from client, on the server it's 4000 or 5000...

      Example Two:

      In this Case readyRead() SIGNAL Is Emited 8 times.

          void Client::SendMessage(std::vector<QString>)
          {
              MyClass _Send;
                      _Send.Age = 22;
                      _Send.School = 14;
                      _Send.Name = "Taz";
      
              QVector<MyClass2> vv;
      
              for (int i = 0; i < 15000; i++) {
                  vv.push_back(MyClass2(24, "leri"));
                  vv.push_back(MyClass2(22, "tazo"));
              }
      
              _Send.vctr = vv;
      
              QByteArray bytes;
              QDataStream stream(&bytes, QIODevice::WriteOnly);
      
              int FunctionUID = 331;
              int ij, ik = ij = 169;
              MyClass2 faf(-31, "15");
      
              stream << FunctionUID << _Send << faf << ij << ik;
      
              socket->write(bytes);
          }
      
      
      
          void Server::ImReady()
          {
              QByteArray buf;
      
              buf = socket->readAll();
      
              QDataStream stream(&buf, QIODevice::ReadOnly);
      
              int FunctionUID, ij, ik;
              MyClass vv;
              MyClass2 vv1;
      
              stream >> FunctionUID >> vv >> vv1 >> ij >> ik;
      
              qDebug() << vv.vctr.size() << "faf";
          }
      
          void Server::incomingConnection(qintptr val)
          {
              qDebug() << "Client Connected" << val;
      
              socket = new QTcpSocket;
              socket->setSocketDescriptor(val);
      
              if (!socket) {
                  return;
              }
      
              connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
              connect(socket, SIGNAL(readyRead()), this, SLOT(ImReady()));
          }
      
      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @Taz742

      QTcpServer has no readyRead signal

      You are probably referring to the readyRead signal emitted by QTcpSocket

      The readyRead is emitted when data is available. Since the socket cannot know when the transmission is finished, it cannot wait until the end of the transmission. Therefore, especially with large amount data received, the signal is triggered, but it is also likely that not all data may caught with a "while (socket->bytesavailable)" and the slot will be finished. However when exitting the slot routine, the event loop shall trigger again the signal.

      For large data amounts this is not a surprise. It all depends on a number of factors such as internet speed and speed of your computer.

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

      L.GogsL 1 Reply Last reply
      3
      • K koahnig

        @Taz742

        QTcpServer has no readyRead signal

        You are probably referring to the readyRead signal emitted by QTcpSocket

        The readyRead is emitted when data is available. Since the socket cannot know when the transmission is finished, it cannot wait until the end of the transmission. Therefore, especially with large amount data received, the signal is triggered, but it is also likely that not all data may caught with a "while (socket->bytesavailable)" and the slot will be finished. However when exitting the slot routine, the event loop shall trigger again the signal.

        For large data amounts this is not a surprise. It all depends on a number of factors such as internet speed and speed of your computer.

        L.GogsL Offline
        L.GogsL Offline
        L.Gogs
        wrote on last edited by
        #3

        @koahnig
        I have the same problem.. so what is solution?

        K jsulmJ 2 Replies Last reply
        0
        • L.GogsL L.Gogs

          @koahnig
          I have the same problem.. so what is solution?

          K Offline
          K Offline
          kuzulis
          Qt Champions 2020
          wrote on last edited by
          #4

          @L.Gogs said in QTcpServer readyRead() Signal emited twice:

          I have the same problem..

          It is not a problem!

          1 Reply Last reply
          2
          • L.GogsL L.Gogs

            @koahnig
            I have the same problem.. so what is solution?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @L.Gogs You need some protocol to know when all data was transferred. For example you can first send the size (number of bytes) of the data you're going to send. Then you send the data. The receiver first reads the size and then reads as long as it received as many bytes as the size was.

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

            K 1 Reply Last reply
            2
            • jsulmJ jsulm

              @L.Gogs You need some protocol to know when all data was transferred. For example you can first send the size (number of bytes) of the data you're going to send. Then you send the data. The receiver first reads the size and then reads as long as it received as many bytes as the size was.

              K Offline
              K Offline
              koahnig
              wrote on last edited by koahnig
              #6

              @jsulm said in QTcpServer readyRead() Signal emited twice:

              @L.Gogs You need some protocol to know when all data was transferred. For example you can first send the size (number of bytes) of the data you're going to send. Then you send the data. The receiver first reads the size and then reads as long as it received as many bytes as the size was.

              To add to this, you may need some sort of a buffer in case you are sending large amounts. It all depends on your goals.

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

              L.GogsL 1 Reply Last reply
              1
              • K koahnig

                @jsulm said in QTcpServer readyRead() Signal emited twice:

                @L.Gogs You need some protocol to know when all data was transferred. For example you can first send the size (number of bytes) of the data you're going to send. Then you send the data. The receiver first reads the size and then reads as long as it received as many bytes as the size was.

                To add to this, you may need some sort of a buffer in case you are sending large amounts. It all depends on your goals.

                L.GogsL Offline
                L.GogsL Offline
                L.Gogs
                wrote on last edited by
                #7

                @koahnig
                I'm using QByteArray

                K 1 Reply Last reply
                0
                • L.GogsL L.Gogs

                  @koahnig
                  I'm using QByteArray

                  K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #8

                  @L.Gogs

                  You can use anything you like for buffering. It is merely a decision for what and how you like to use it later on. Buffering is more a term for being aware that you may have to keep a copy of what you have received so far.

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

                  L.GogsL 1 Reply Last reply
                  0
                  • K koahnig

                    @L.Gogs

                    You can use anything you like for buffering. It is merely a decision for what and how you like to use it later on. Buffering is more a term for being aware that you may have to keep a copy of what you have received so far.

                    L.GogsL Offline
                    L.GogsL Offline
                    L.Gogs
                    wrote on last edited by
                    #9

                    @koahnig
                    Ok. Thanks :)

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Hi
                      just as note to this.
                      The classic error is to do
                      void DataReadSignal()
                      {
                      QByteArray buf;
                      buf = socket->readAll(); // it might not read all in one go.
                      }
                      where it should have been
                      buf += socket->readAll();
                      if (buf.size() >= expected_size) ProcessData();

                      and QByteArray buf; should be a member of the class, not local variable.

                      1 Reply Last reply
                      3
                      • Taz742T Taz742

                        I have QTcpServer. I want to send large data from client side and how to catch signal, when all data is received on server?
                        "while (socket->bytesavailable)" doesn't work.

                        For example:

                        when qbytearray size is 9000, which is send from client, on the server it's 4000 or 5000...

                        Example Two:

                        In this Case readyRead() SIGNAL Is Emited 8 times.

                            void Client::SendMessage(std::vector<QString>)
                            {
                                MyClass _Send;
                                        _Send.Age = 22;
                                        _Send.School = 14;
                                        _Send.Name = "Taz";
                        
                                QVector<MyClass2> vv;
                        
                                for (int i = 0; i < 15000; i++) {
                                    vv.push_back(MyClass2(24, "leri"));
                                    vv.push_back(MyClass2(22, "tazo"));
                                }
                        
                                _Send.vctr = vv;
                        
                                QByteArray bytes;
                                QDataStream stream(&bytes, QIODevice::WriteOnly);
                        
                                int FunctionUID = 331;
                                int ij, ik = ij = 169;
                                MyClass2 faf(-31, "15");
                        
                                stream << FunctionUID << _Send << faf << ij << ik;
                        
                                socket->write(bytes);
                            }
                        
                        
                        
                            void Server::ImReady()
                            {
                                QByteArray buf;
                        
                                buf = socket->readAll();
                        
                                QDataStream stream(&buf, QIODevice::ReadOnly);
                        
                                int FunctionUID, ij, ik;
                                MyClass vv;
                                MyClass2 vv1;
                        
                                stream >> FunctionUID >> vv >> vv1 >> ij >> ik;
                        
                                qDebug() << vv.vctr.size() << "faf";
                            }
                        
                            void Server::incomingConnection(qintptr val)
                            {
                                qDebug() << "Client Connected" << val;
                        
                                socket = new QTcpSocket;
                                socket->setSocketDescriptor(val);
                        
                                if (!socket) {
                                    return;
                                }
                        
                                connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
                                connect(socket, SIGNAL(readyRead()), this, SLOT(ImReady()));
                            }
                        
                        Taz742T Offline
                        Taz742T Offline
                        Taz742
                        wrote on last edited by
                        #11

                        Thank you all.

                        void Client::SendMessage(std::vector<QString>)
                        {
                            MyClass _Send;
                                    _Send.Age = 22;
                                    _Send.School = 14;
                                    _Send.Name = "Taz";
                        
                            QVector<MyClass2> vv;
                        
                            for (int i = 0; i < 15000; i++) {
                                vv.push_back(MyClass2(24, "leri"));
                                vv.push_back(MyClass2(22, "tazo"));
                            }
                        
                            _Send.vctr = vv;
                        
                            QByteArray bytes;
                            QDataStream stream(&bytes, QIODevice::WriteOnly);
                        
                            int FunctionUID = 3311;
                            int ij, ik = ij = 169;
                            MyClass2 faf(-31, "15");
                        
                            stream << FunctionUID << _Send << faf << ij << ik;
                        
                            QString sz = QString::number(bytes.size() + 10);
                        
                            while (sz.size() != 10) {
                                sz += " ";
                            }
                        
                            bytes.insert(0, sz);
                        
                            qDebug() << bytes.size() << "ss";
                        
                            socket->write(bytes);
                        
                            socket->flush();
                        }
                        
                        void Server::ImReady()
                        {
                            QString foo = "";
                        
                            buf += socket->readAll();
                        
                            for (int i = 0; i < 10; i++) {
                                foo += buf[i];
                            }
                        
                        
                            if (buf.size() < foo.toInt()) {
                                qDebug() << "jer adrea" << buf.size() << foo.toInt();
                                return;
                            }
                        
                            qDebug() << "mzadaa" << buf.size();
                            
                            QDataStream stream(&buf, QIODevice::ReadOnly);
                        
                        //    int l, ij, ik;
                        //    MyClass vv;
                        //    MyClass2 vv1;
                        
                        //    stream >> l >> vv >> vv1 >> ij >> ik;
                        
                        
                        //    qDebug() << vv.vctr.size() << l;
                        }
                        

                        Do what you want.

                        1 Reply Last reply
                        0
                        • mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Hi
                          To be on the sure side, i would do

                          for (int i = 0; i < 10; i++) {
                          foo += buf[i];
                          }
                          -->
                          if( buf.size() > 10 )
                          for (int i = 0; i < 10; i++) {
                          foo += buf[i];
                          }

                          else you would go out of range with buf[i]; in case a small data set was sent.

                          1 Reply Last reply
                          1

                          • Login

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