Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to send the data from the server to the client continuously, every 1000 ms under TCP/IP , using QTcpsocket and QTcpserver
Qt 6.12 LTS is out! See what's new in the release blog

how to send the data from the server to the client continuously, every 1000 ms under TCP/IP , using QTcpsocket and QTcpserver

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 2 Posters 4.7k Views
  • 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.
  • A Alex42

    @KroMignon , thank you for the response ,Maybe I have to change the structure of my class, because I had tested several times but nothing that works.

    KroMignonK Offline
    KroMignonK Offline
    KroMignon
    wrote on last edited by KroMignon
    #4

    @Alex42 said in how to send the data from the server to the client continuously, every 1000 ms under TCP/IP , using QTcpsocket and QTcpserver:

    thank you for the response ,Maybe I have to change the structure of my class, because I had tested several times but nothing that works.

    It is not a structure problem, it does what you have coded!

    server = new QTcpServer(this);
    connect(server, &QTcpServer::newConnection,this, &Calibration::newConnections, Qt::QueuedConnection);
    
    timer = new QTimer(this);
    connect(timer, QTimer::timeout, this, &Calibration::keepAlive, Qt::QueuedConnection);
    timer->start(1000);
    
    if (!server ->listen(QHostAddress::Any,9000))
    {
        qDebug () << "Server could not start !";
    }
    else
    {
        qDebug () << "Server started" ;
    }
    
    void Calibration::newConnections ()
    {
       while(server->hasPendingConnections())
       {
            QTcpSocket *socket = server->nextPendingConnection();
        
            // add socket to client list (QList<QTcpSocket *> mClients (class member)
            mClients << socket;
    
            // remove socket from list on disconnection
            connect(socket, &QTcpSocket::disconnected, this, [this, socket]() {
                mClients.removeAll(socket);
            }, Qt::QueuedConnection);
    
            QTextStream T(socket);
            T <<"&& \r\n"<<"2608"<<(ui->lineEdit_17->text())<<"\r\n"<<"2609"<<(ui->lineEdit_17->text())<<"\r\n" <<"2610"<<( ui->lineEdit_17->text())<< "\r\n!!" ;
            socket->flush();
        }
    }
    
    void Calibration::keepAlive()
    {
        for(const auto socket : mClients)
        {
            QTextStream T(socket);
            T <<"KeepAlive\r\n!!" ;
            socket->flush();
        }
    }
    

    Note: I use QueuedConnection to avoid threading issues and not have to add locker/mutex around the QList insert/read/remove items.

    EDIT: add while loop to handle pending connections, is better because their can be more than one client requesting connection to server!

    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

    1 Reply Last reply
    4
    • A Offline
      A Offline
      Alex42
      wrote on last edited by
      #5

      unfortunately i have the same probleme

        server = new QTcpServer(this);
        connect(server, &QTcpServer::newConnection,this, &Calibration::newConnections, Qt::QueuedConnection);
      
        timer = new QTimer(this);
        connect(timer, SIGNAL (timeout()), this, SLOT(keepAliv()));
        timer->start(1000);
      
        if (!server ->listen(QHostAddress::Any,9000))
        {
            qDebug () << "Server could not start !";
        }
        else
        {
            qDebug () << "Server started" ;
        }
      

      QList<QTcpSocket *>mClients ;

        }
      
        void Calibration::newConnections ()
        {
      
            QTcpSocket *socket = server->nextPendingConnection();
      
                // add socket to client list (QList<QTcpSocket *> mClients (class member)
        QList<QTcpSocket *>mClients ;
                mClients << socket;
      
                // remove socket from list on disconnection
                connect(socket, &QTcpSocket::disconnected, this, [this, socket]() {
                 ;
                }, Qt::QueuedConnection);
      
                QTextStream T(socket);
                T <<"&& \r\n"<<"2608"<<(ui->lineEdit_17->text())<<"\r\n"<<"2609"<<(ui->lineEdit_17->text())<<"\r\n" <<"2610"<<( ui->lineEdit_17->text())<< "\r\n!!" ;
                socket->flush();
      
      ![alt text](image url)
      
        }
      
        void Calibration::keepAlive()
        {
             QList<QTcpSocket *>mClients ;
            for(const auto socket : mClients)
            {
                QTextStream T(socket);
                T <<"KeepAlive\r\n!!" ;
                socket->flush();
            }
        }
      

      telnet.PNG

      KroMignonK 1 Reply Last reply
      0
      • A Alex42

        unfortunately i have the same probleme

          server = new QTcpServer(this);
          connect(server, &QTcpServer::newConnection,this, &Calibration::newConnections, Qt::QueuedConnection);
        
          timer = new QTimer(this);
          connect(timer, SIGNAL (timeout()), this, SLOT(keepAliv()));
          timer->start(1000);
        
          if (!server ->listen(QHostAddress::Any,9000))
          {
              qDebug () << "Server could not start !";
          }
          else
          {
              qDebug () << "Server started" ;
          }
        

        QList<QTcpSocket *>mClients ;

          }
        
          void Calibration::newConnections ()
          {
        
              QTcpSocket *socket = server->nextPendingConnection();
        
                  // add socket to client list (QList<QTcpSocket *> mClients (class member)
          QList<QTcpSocket *>mClients ;
                  mClients << socket;
        
                  // remove socket from list on disconnection
                  connect(socket, &QTcpSocket::disconnected, this, [this, socket]() {
                   ;
                  }, Qt::QueuedConnection);
        
                  QTextStream T(socket);
                  T <<"&& \r\n"<<"2608"<<(ui->lineEdit_17->text())<<"\r\n"<<"2609"<<(ui->lineEdit_17->text())<<"\r\n" <<"2610"<<( ui->lineEdit_17->text())<< "\r\n!!" ;
                  socket->flush();
        
        ![alt text](image url)
        
          }
        
          void Calibration::keepAlive()
          {
               QList<QTcpSocket *>mClients ;
              for(const auto socket : mClients)
              {
                  QTextStream T(socket);
                  T <<"KeepAlive\r\n!!" ;
                  socket->flush();
              }
          }
        

        telnet.PNG

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by KroMignon
        #6

        @Alex42 said in how to send the data from the server to the client continuously, every 1000 ms under TCP/IP , using QTcpsocket and QTcpserver:

        unfortunately i have the same probleme

        No, the problem is different: your connect statement is wrong! You try to connect to slot keepAliv() which not exists, it is keepAlive().
        To avoid this kind of error, please use new connect syntax:

        // WRONG
        connect(timer, SIGNAL (timeout()), this, SLOT(keepAliv()));
        
        // CORRECT
        connect(timer, SIGNAL (timeout()), this, SLOT(keepAlive()));
        
        // EVEN MORE BETTER
        connect(timer, &QTimer::timeout, this, &Calibration::keepAlive);
        

        And, as I write in my previous post. I used Qt::QueuedConnection to avoid thread issues and not have to add locker/mutex around list access.

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply
        2
        • A Offline
          A Offline
          Alex42
          wrote on last edited by
          #7

          it's the same problem, but I think the problem come from the dynamic array QList<QTcpSocket *> mClients
          erreur "use of undeclared identifier mClients

          void Calibration::newConnections ()
                {
          
                    QTcpSocket *socket = server->nextPendingConnection();
          
                        // add socket to client list (QList<QTcpSocket *> mClients (class member)
             
                        mClients << socket;
          
                        // remove socket from list on disconnection
                        connect(socket, &QTcpSocket::disconnected, this, [this, socket]() {
                                   mClients.removeAll(socket);
                               }, Qt::QueuedConnection);
          
                        QTextStream T(socket);
                        T <<"&& \r\n"<<"2608"<<(ui->lineEdit_17->text())<<"\r\n"<<"2609"<<(ui->lineEdit_17->text())<<"\r\n" <<"2610"<<( ui->lineEdit_17->text())<< "\r\n!!" ;
                        socket->flush();
          
          
                }
          
                void Calibration::keepAlive()
                {
                
                    for(const auto socket : mClients)
                    {
                        QTextStream T(socket);
                        T <<"KeepAlive\r\n!!" ;
                        socket->flush();
                    }
                }
          
          KroMignonK 1 Reply Last reply
          0
          • A Alex42

            it's the same problem, but I think the problem come from the dynamic array QList<QTcpSocket *> mClients
            erreur "use of undeclared identifier mClients

            void Calibration::newConnections ()
                  {
            
                      QTcpSocket *socket = server->nextPendingConnection();
            
                          // add socket to client list (QList<QTcpSocket *> mClients (class member)
               
                          mClients << socket;
            
                          // remove socket from list on disconnection
                          connect(socket, &QTcpSocket::disconnected, this, [this, socket]() {
                                     mClients.removeAll(socket);
                                 }, Qt::QueuedConnection);
            
                          QTextStream T(socket);
                          T <<"&& \r\n"<<"2608"<<(ui->lineEdit_17->text())<<"\r\n"<<"2609"<<(ui->lineEdit_17->text())<<"\r\n" <<"2610"<<( ui->lineEdit_17->text())<< "\r\n!!" ;
                          socket->flush();
            
            
                  }
            
                  void Calibration::keepAlive()
                  {
                  
                      for(const auto socket : mClients)
                      {
                          QTextStream T(socket);
                          T <<"KeepAlive\r\n!!" ;
                          socket->flush();
                      }
                  }
            
            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by KroMignon
            #8

            @Alex42 said in how to send the data from the server to the client continuously, every 1000 ms under TCP/IP , using QTcpsocket and QTcpserver:

            erreur "use of undeclared identifier mClients

            As written in my second post, you have to declare mClients as class member of Calibration.
            I suppose that Calibration is based on QObject:

            class Calibration : public QObject
            {
                Q_OBJECT
            
            public:
                explicit Calibration(QObject * parent = 0):
            ....
            private:
                QList<QTcpSocket*> mClients;
            };
            

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            2
            • A Offline
              A Offline
              Alex42
              wrote on last edited by
              #9

              OK resolved , but another probleme in the slot keepAlive() " no matching constuctor forinitilisation of "QTextStream"

              void Calibration::keepAlive()
                    {
              
                        for(const auto socket : mClients)
                        {
                            QTextStream T(socket);
                            T <<"KeepAlive\r\n!!" ;
                            socket->flush();
                        }
                    }
              
              KroMignonK 1 Reply Last reply
              0
              • A Alex42

                OK resolved , but another probleme in the slot keepAlive() " no matching constuctor forinitilisation of "QTextStream"

                void Calibration::keepAlive()
                      {
                
                          for(const auto socket : mClients)
                          {
                              QTextStream T(socket);
                              T <<"KeepAlive\r\n!!" ;
                              socket->flush();
                          }
                      }
                
                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by KroMignon
                #10

                @Alex42 said in how to send the data from the server to the client continuously, every 1000 ms under TCP/IP , using QTcpsocket and QTcpserver:

                OK resolved , but another probleme in the slot keepAlive() "

                Yes, my fault, I correct my previous post: mClients have to be declared as QList<QTcpSocket*> not QList<QObjet*>

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                1
                • A Offline
                  A Offline
                  Alex42
                  wrote on last edited by
                  #11

                  the problem is resolved, You are The Best, Thank You Very Much for Your Help.

                  KroMignonK 2 Replies Last reply
                  0
                  • A Alex42

                    the problem is resolved, You are The Best, Thank You Very Much for Your Help.

                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on last edited by
                    #12

                    @Alex42 said in how to send the data from the server to the client continuously, every 1000 ms under TCP/IP , using QTcpsocket and QTcpserver:

                    Thank You Very Much for Your Help.

                    your welcome

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    1 Reply Last reply
                    0
                    • A Alex42

                      the problem is resolved, You are The Best, Thank You Very Much for Your Help.

                      KroMignonK Offline
                      KroMignonK Offline
                      KroMignon
                      wrote on last edited by
                      #13

                      @Alex42 said in how to send the data from the server to the client continuously, every 1000 ms under TCP/IP , using QTcpsocket and QTcpserver:

                      the problem is resolved,

                      After rereading my code, I found a last little error.
                      There is a memory leak in my code, after socket is removed from mClientList it also have to deleted:

                      // remove socket from list on disconnection and free memory
                      connect(socket, &QTcpSocket::disconnected, this, [this, socket]() {
                               mClients.removeAll(socket);
                               socket->delateLater();
                           }, Qt::QueuedConnection);
                      

                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        Alex42
                        wrote on last edited by
                        #14

                        @KroMignon said in how to send the data from the server to the client continuously, every 1000 ms under TCP/IP , using QTcpsocket and QTcpserver:

                        socket->delateLater();

                        deleteLater(), I added it and everything works wonderfully, thank you again.

                        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