Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. TCP Server/Client Application
Forum Updated to NodeBB v4.3 + New Features

TCP Server/Client Application

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 8Observer88 Offline
    8Observer88 Offline
    8Observer8
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • 8Observer88 Offline
        8Observer88 Offline
        8Observer8
        wrote on last edited by
        #3

        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&#40;&#41;;
        

        }@

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

        }@

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          But what is your problem?
          Where do you receive already information?

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved