Hi,
My Server receives data (integer values) from the Clients (I called them "Drivers"). How to send data to the Viewer?
You need to run the Server at first.
Thank you!
Printable View
Hi,
My Server receives data (integer values) from the Clients (I called them "Drivers"). How to send data to the Viewer?
You need to run the Server at first.
Thank you!
What is the actual problem?
My Driver01 sends the peckage to the Server every second:
Code:
void Driver01::timeOut() { QByteArray buffer; buffer.append("Driver"); buffer.append(" "); buffer.append("01"); buffer.append(" "); if(socket->isWritable()) { socket->write(buffer); } value++; qDebug() << "Driver01" << buffer; }
The Server receives the data. I want to send this data to the Viewer (Viewer is the GUI application).
Server
Code:
#include <QCoreApplication> #include "server.h" int main(int argc, char *argv[]) { Server server; server.startServer(); return a.exec(); }
server.cpp
Code:
#include "server.h" #include "client.h" #include <QDebug> { } void Server::startServer() { qDebug() << "Server started"; } else { qDebug() << "Server could not start"; } } void Server::incomingConnection(int socketDescriptor) { Client *client = new Client(this); client->setSocket(socketDescriptor); }
client.cpp
Code:
#include "client.h" #include "viewertask.h" #include "drivertask.h" #include <QThreadPool> #include <QDebug> #include <QStringList> { QThreadPool::globalInstance()->setMaxThreadCount(11); } void Client::setSocket(int socketDescriptor) { connect(socket, SIGNAL(connected()), this, SLOT(connected())); connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected())); connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); socket->setSocketDescriptor(socketDescriptor); qDebug() << socketDescriptor << " client connected"; } void Client::connected() { qDebug() << "Client connected event"; } void Client::disconnected() { qDebug() << "Client disconnected event"; } void Client::readyRead() { QByteArray buffer; buffer = socket->readAll(); if (package.isEmpty()) { return; } // Time Consumer if (name == "Driver") { DriverTask *driverTask = new DriverTask(elements); driverTask->setAutoDelete(true); QThreadPool::globalInstance()->start(driverTask); } else { ViewerTask *viewerTask = new ViewerTask; viewerTask->setAutoDelete(true); connect(viewerTask, SIGNAL(result(int)), this, SLOT(taskResult(int)), Qt::QueuedConnection); QThreadPool::globalInstance()->start(viewerTask); } } void Client::taskResult(int number) { QByteArray buffer; buffer.append("- - - - - - - - - -"); //buffer.append(QString::number(number)); socket->write(buffer); } { // QByteArray buffer; // buffer.append(name); // buffer.append(""); // buffer.append(QString::number(id)); // buffer.append(""); // buffer.append(QString::number(number)); }
drivertask.cpp
Code:
#include "drivertask.h" #include <QDebug> { this->name = elements[0]; this->id = elements[1].toInt(); this->value = elements[2].toInt(); } void DriverTask::run() { emit result(name, id, value); }
viewertask.cpp
Code:
#include "viewertask.h" #include <QDebug> ViewerTask::ViewerTask() { } void ViewerTask::run() { // time consumer qDebug() << "Viewer Task Start"; int iNumber = 0; for (int i = 0; i < 100; i++) { iNumber +=i; } qDebug() << "Viewer Task Done"; emit result(iNumber); }
Can't you send it the same way you received it?
By the way, your client's readyRead() code is invalid. You are likely to miss some data there.
I cannot because the Viewer and the Driver are the different Client-objects:
Code:
void Server::incomingConnection(int socketDescriptor) { Client *client = new Client(this); client->setSocket(socketDescriptor); }
How to get the data from the Client-Driver for the Client-Viewer?
Sorry, I don't understand. If you use protocol X between points A and B, why can't you use the same protocol between points B and C?
I don't really see the point of your thread here. Do you want us to invent some mystical hypothetical communication protocol between two (or more) unidentified, magical, hidden, maybe crouching, maybe shy, maybe legendary entities? If you have a specific question then ask it. If you have a specific problem, then describe it and then say what kind of help you expect. If you have an unidentified problem, identify it first.