I am writing a prototype client-server application; in combat mode, the application should transfer ~ 200 values of the form QString, QVariant about 60 times per second.
The problem is that at this rate of sending on the client, I successfully receive about one data QVariant::invalid out of 50, otherwise the variables taken from the stream are disabled ( QVariant::invalid )
Code for server:
void TDummyWorker::initialize() { timer = new QTimer(this); timer->setInterval(10); connect(timer,SIGNAL(timeout()),this,SLOT(foo())); //будем посылать данные с заданным интервалом timer->start(); server = new QTcpServer(this); connect(server,&QTcpServer::newConnection,this,&TDummyWorker::somebodyConnected); server->listen(QHostAddress::Any, 33333); } void TDummyWorker::foo() { QVector <TNamedVariable> vars; static int n=0; int ndata = 200; for (int i=0; i<ndata; i++) { //формируем фейковые данные vars.append(TNamedVariable("variable_number_" % QString::number(i), (i%2 != 0 ? float((i+n)*0.01) : int(i+n)) )); } for (QMap<int ,QTcpSocket *>::iterator it = clients.begin(); it!=clients.end(); ++it) { sendToClient(*it, vars); //посылаем данные всем подключенным клиентам } n++; if (n>1000000) n=0; } void TDummyWorker::sendToClient(QTcpSocket *socket, QVector <TNamedVariable> &vars) { QByteArray arr; QDataStream out(&arr, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_5_2); int n=vars.size(); out << quint16(0); for(int i=0; i<n;i++) { out << vars[i]; } out.device()->seek(0); out << quint16(arr.size() - sizeof(quint16)); sendMessage(vars[0].value.toString() % " " % QString::number(arr.size() - sizeof(quint16))); socket->write(arr); } And this is how I get data on the client side:
void TClient::recieveData() { QDataStream stream(socket); stream.setVersion(QDataStream::Qt_5_2); QVector <TNamedVariable> vars; quint16 nextBlockSize=0; while (true) { if (nextBlockSize==0) { if (socket->bytesAvailable() < sizeof(quint16)) { break; } stream >> nextBlockSize; } if (socket->bytesAvailable() < nextBlockSize) break; TNamedVariable buf; while (!stream.atEnd()) { stream >> buf; buf.debug(); vars.append(buf); } nextBlockSize = 0; } emit sendData(vars); } If I send 1-2 variables once a second, then on the client side everything is accepted normally, the more I increase the transmission frequency and the number of variables in the vector, the more broken data I get. I suspect that the error is rather trivial, because the code is almost entirely from the examples from the textbook.
Correctly working code after all edits:
void TClient::recieveData() { QDataStream stream(socket); stream.setVersion(QDataStream::Qt_5_2); static int nbad = 0; TNamedVariable buf; static quint16 nextBlockSize=0; while (true) { if (nextBlockSize == 0) { if (socket->bytesAvailable() < sizeof(quint16)) { break; } stream >> nextBlockSize; } if (socket->bytesAvailable() < nextBlockSize) { break; } while (!stream.atEnd()) { stream >> buf; if (!buf.value.isValid()) { qDebug()<<"op :("<<++nbad; } if (buf.name == "eod") break; vars.append(buf); } if (buf.name == "eod") { emit sendData(vars); vars.clear(); } nextBlockSize = 0; } }