how to send the data from the server to the client continuously, every 1000 ms under TCP/IP , using QTcpsocket and QTcpserver
-
hello every one , i have an interface developed under c++ qt cretore (lineEdite ), i want to create a server that he send the data to the client every 1000ms with TCP/IP,
unfortunatly after the creation of my server and testing him withe a telnet client , I receive the data only once, but not every 1000 ms
maybe some one can helpe me to resolve the problem , thank you in advance.
attached a piece of my server code
server.cppserver = new QTcpServer (this); connect (server,SIGNAL (newConnection ()),this,SLOT(newConnections())); timer =new QTimer(this); timer ->start(1000); connect (timer, SIGNAL (timeout()), this ,SLOT (newConnections(())); if (!server ->listen(QHostAddress::Any,9000)) { qDebug () << "Server could not start !"; } else { //connect (timer,SIGNAL (timeout()),this,SLOT (newConnection ())); qDebug () << "Server started" ; } void Calibration::newConnections () { QTcpSocket *socket = server->nextPendingConnection(); socket ->waitForBytesWritten(2000); 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!!" ; //T <<"\r\n&& \r\n"<<(ui->lineEdit_17->text()) <<"!!\r\n!!" ; socket ->flush();
-
@Alex42 It is not a surprise that the code does not what you expect. Because it is not what you have coded.
The timer you have create will check every second if there is a new pending connection, it does not send new data on existing connection.
-
@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.
-
@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!
-
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(); } }
-
@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 iskeepAlive()
.
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's the same problem, but I think the problem come from the dynamic array QList<QTcpSocket *> mClients
erreur "use of undeclared identifier mClientsvoid 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(); } }
-
@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 ofCalibration
.
I suppose thatCalibration
is based on QObject:class Calibration : public QObject { Q_OBJECT public: explicit Calibration(QObject * parent = 0): .... private: QList<QTcpSocket*> mClients; };
-
-
@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*>
notQList<QObjet*>
-
@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);
-
@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.