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
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();  } void Calibration::keepAlive() { QList<QTcpSocket *>mClients ; for(const auto socket : mClients) { QTextStream T(socket); T <<"KeepAlive\r\n!!" ; socket->flush(); } } -
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();  } 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::QueuedConnectionto 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(); } } -
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
mClientsas class member ofCalibration.
I suppose thatCalibrationis based on QObject:class Calibration : public QObject { Q_OBJECT public: explicit Calibration(QObject * parent = 0): .... private: QList<QTcpSocket*> mClients; }; -
-
OK resolved , but another probleme in the slot keepAlive() " no matching constuctor forinitilisation of "QTextStream"
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:
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.