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. Send packet over qtcSocket continuously

Send packet over qtcSocket continuously

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.0k 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.
  • zhmhZ Offline
    zhmhZ Offline
    zhmh
    wrote on last edited by zhmh
    #1

    Hi,
    I have gui program have start and stop button , if clicked start send packet over qtcpSocket
    until click stop, I do this with timer
    click start->start timer->send packet every 3 second
    click stop->stop timer->stop send packet
    it's work fine until stop and click start again packet send twice
    for example if first time click start send "hello" then stop and
    click start again send "hellohello" ;
    why this happening?? is there any way instead of timer to send packet continuously??

    GUI::GUI(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::GUI)
    {
        ui->setupUi(this);
        send=new SendCommand();
        _server=new QTcpServer(this);
        tm=new QTimer(this);
    }
    void GUI::on_start_clicked()
    {
        connect(tm, SIGNAL(timeout()),this, SLOT(sendInventory()));
        tm->start(3000);
    }
    void GUI::on_stop_clicked()
    {
        tm->stop();
      }
    
    int GUI::SendTCP(QByteArray Data)
    {
        bool connected = (_socket->state() == QTcpSocket::ConnectedState);
        if(connected){
            _socket->write(Data, Data.size());
            qDebug()<<"Data send"<<Data.toHex();
            return _socket->waitForBytesWritten(3000);
        }
        else{
            return false;
        }
    
    }
    void GUI::sendInventory()
    {
        SendTCP(//something);
     
    }
    

    Best Regards.

    K 1 Reply Last reply
    0
    • zhmhZ zhmh

      Hi,
      I have gui program have start and stop button , if clicked start send packet over qtcpSocket
      until click stop, I do this with timer
      click start->start timer->send packet every 3 second
      click stop->stop timer->stop send packet
      it's work fine until stop and click start again packet send twice
      for example if first time click start send "hello" then stop and
      click start again send "hellohello" ;
      why this happening?? is there any way instead of timer to send packet continuously??

      GUI::GUI(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::GUI)
      {
          ui->setupUi(this);
          send=new SendCommand();
          _server=new QTcpServer(this);
          tm=new QTimer(this);
      }
      void GUI::on_start_clicked()
      {
          connect(tm, SIGNAL(timeout()),this, SLOT(sendInventory()));
          tm->start(3000);
      }
      void GUI::on_stop_clicked()
      {
          tm->stop();
        }
      
      int GUI::SendTCP(QByteArray Data)
      {
          bool connected = (_socket->state() == QTcpSocket::ConnectedState);
          if(connected){
              _socket->write(Data, Data.size());
              qDebug()<<"Data send"<<Data.toHex();
              return _socket->waitForBytesWritten(3000);
          }
          else{
              return false;
          }
      
      }
      void GUI::sendInventory()
      {
          SendTCP(//something);
       
      }
      

      Best Regards.

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

      @zhmh

      Each time you will add another connection to your timer.

          tm=new QTimer(this);
          connect(tm, SIGNAL(timeout()),this, SLOT(sendInventory()));
      }
      void GUI::on_start_clicked()
      {
          // connect(tm, SIGNAL(timeout()),this, SLOT(sendInventory()));  <<<< moved up
          tm->start(3000);
      }
      

      Moving up the connect statement should do the trick.

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

      1 Reply Last reply
      4
      • zhmhZ Offline
        zhmhZ Offline
        zhmh
        wrote on last edited by
        #3

        sometimes the problem just in front of the eyes ,but can't see it..... :|
        tnx for reply :)

        K 1 Reply Last reply
        1
        • zhmhZ zhmh

          sometimes the problem just in front of the eyes ,but can't see it..... :|
          tnx for reply :)

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

          @zhmh
          :D Happens to all of us!

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

          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 Extensions
          • Unsolved