PDA

View Full Version : TCP Server/Client Application



8Observer8
4th September 2013, 16:40
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!

wysota
4th September 2013, 19:27
What is the actual problem?

8Observer8
4th September 2013, 20:18
My Driver01 sends the peckage to the Server every second:



void Driver01::timeOut()
{
QByteArray buffer;
buffer.append("Driver");
buffer.append(" ");
buffer.append("01");
buffer.append(" ");
buffer.append(QString::number(value));
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


#include <QCoreApplication>
#include "server.h"

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

Server server;
server.startServer();

return a.exec();
}


server.cpp


#include "server.h"
#include "client.h"
#include <QDebug>

Server::Server(QObject *parent) :
QTcpServer(parent)
{
}

void Server::startServer()
{
if (this->listen(QHostAddress::Any, 1234)) {
qDebug() << "Server started";
}
else {
qDebug() << "Server could not start";
}
}

void Server::incomingConnection(int socketDescriptor) {
Client *client = new Client(this);
client->setSocket(socketDescriptor);
}


client.cpp


#include "client.h"
#include "viewertask.h"
#include "drivertask.h"
#include <QThreadPool>
#include <QDebug>
#include <QStringList>

Client::Client(QObject *parent) :
QObject(parent)
{
QThreadPool::globalInstance()->setMaxThreadCount(11);
}

void Client::setSocket(int socketDescriptor)
{
socket = new QTcpSocket(this);

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();

QString package(buffer);
if (package.isEmpty()) {
return;
}

QStringList elements = package.split(" ", QString::SkipEmptyParts);
QString name = elements[0];

// Time Consumer
if (name == "Driver") {
DriverTask *driverTask = new DriverTask(elements);
driverTask->setAutoDelete(true);

connect(driverTask, SIGNAL(result(QString, int, int)), this, SLOT(taskDriverResult(QString, int, int)), Qt::QueuedConnection);

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);
}

void Client::taskDriverResult(QString name, int id, int number)
{
qDebug() << name << " " << QString::number(id) << " " << QString::number(number);
// QByteArray buffer;
// buffer.append(name);
// buffer.append("");
// buffer.append(QString::number(id));
// buffer.append("");
// buffer.append(QString::number(number));
}


drivertask.cpp


#include "drivertask.h"
#include <QDebug>

DriverTask::DriverTask(QStringList elements, QObject *parent) :
QObject(parent)
{
this->name = elements[0];
this->id = elements[1].toInt();
this->value = elements[2].toInt();
}

void DriverTask::run()
{
emit result(name, id, value);
}


viewertask.cpp


#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);
}

wysota
4th September 2013, 20:24
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.

8Observer8
4th September 2013, 20:31
I cannot because the Viewer and the Driver are the different Client-objects:



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?

wysota
4th September 2013, 20:38
I cannot because the Viewer and the Driver are the different Client-objects:
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.