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
Forum Updated to NodeBB v4.3 + New Features

Send packet over qtcSocket continuously

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.1k 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.
  • Z Offline
    Z Offline
    zhmh
    wrote on 30 May 2016, 14:59 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 30 May 2016, 15:28
    0
    • Z zhmh
      30 May 2016, 14:59

      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 30 May 2016, 15:28 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
      • Z Offline
        Z Offline
        zhmh
        wrote on 1 Jun 2016, 08:28 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 Jun 2016, 09:00
        1
        • Z zhmh
          1 Jun 2016, 08:28

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

          K Offline
          K Offline
          koahnig
          wrote on 1 Jun 2016, 09:00 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

          1/4

          30 May 2016, 14:59

          • Login

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