Send packet over qtcSocket continuously
-
wrote on 30 May 2016, 14:59 last edited by 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.
-
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.
wrote on 30 May 2016, 15:28 last edited by koahnigEach 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.
-
wrote on 1 Jun 2016, 08:28 last edited by
sometimes the problem just in front of the eyes ,but can't see it..... :|
tnx for reply :)
1/4