TCP Server/Client Application
-
Hi,
My Server receives data (integer values) from the Clients (I called them "Drivers"). How to send data to the Viewer?
How to attach my .zip archives?
Thank you!
-
Did you check out already the fortune "server":http://qt-project.org/doc/qt-5.0/qtnetwork/fortuneserver.html and "client":http://qt-project.org/doc/qt-5.0/qtnetwork/fortuneclient.html examples?
They show also how to manage displaying the information.The established connection does not care what you are sending over. There is no file management or anything. You have to take care of all this yourself. E.g. you can send over the file name and the file size followed by all bytes of your file. On the other side you will receive the information and assemble the file again.
-
Thank you. I will.
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 consumerqDebug() << "Viewer Task Start"; int iNumber = 0; for (int i = 0; i < 100; i++) { iNumber +=i; } qDebug() << "Viewer Task Done"; emit result(iNumber);
}@
-
But what is your problem?
Where do you receive already information?