Send packet over qtcSocket continuously



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


  • Moderators

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



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


  • Moderators

    @zhmh
    :D Happens to all of us!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.