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. multi threading send data to clients
Forum Updated to NodeBB v4.3 + New Features

multi threading send data to clients

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 882 Views 2 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.
  • M Offline
    M Offline
    Mogli123
    wrote on last edited by Mogli123
    #1

    Hi,

    I would to send datas (QString) from a qt-project to another, via qtcpserver.
    I have the code from the following example
    QTcpServer Multithreaded Client
    but I don't know how to send my data to the clients.
    I can only send data once at the beginning when the client is connecting to the server.
    I hope somewone can giv me a tip or a pice of code.

    This is my code.

    mytcpserver.h

    #include <QObject>
    #include <QTcpServer>
    #include <QtDebug>
    #include "mythread.h"
    
    class MyTcpServer : public QTcpServer
    {
        Q_OBJECT
    public:
        explicit MyTcpServer(QObject *parent = nullptr);
        void StartServer();
    
    signals:
        void sendData(QString sendData);
    
    public slots:
        void dataToSend(QString dataToSend);
    
    
    private:
        MyThread *mThread;
    
    protected:
        void incomingConnection(qintptr socketDescriptor);
    
    

    mythread.h

    #include <QObject>
    #include <QThread>
    #include <QTcpSocket>
    #include <QDebug>
    
    class MyThread : public QThread
    {
        Q_OBJECT
    public:
        explicit MyThread(int ID, QObject *parent = nullptr);
        void run();
    
    signals:
        void error(QTcpSocket::SocketError socketerror);
    
    
    public slots:
        void readyRead();
        void disconnected();
    
    private:
        QTcpSocket *client;
        int socketDescriptor;
    
    };
    

    myserver.cpp

    MyTcpServer::MyTcpServer(QObject *parent) :
        QTcpServer(parent)
    {
    }
    
    void MyTcpServer::StartServer()
    {
        if (!this->listen(QHostAddress::Any,5000))
        {
            qDebug()<< "second Could not start server";
        }
        else
        {
         qDebug()<<"Listening...";
        }
    }
    
    void MyTcpServer::incomingConnection(qintptr socketDescriptor)
    {
        qDebug()<< socketDescriptor<<"Connecting...";
        MyThread *thread = new MyThread(socketDescriptor, this);
        connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
        thread->start();
    }
    
    void MyTcpServer::dataToSend(QString toSendData)
    {
        mclient->waitForReadyRead(300);
    //  schickt den empfangenen String an die Verbundenen Clients
        mclient->write("Pushservice TCP-Client:" + toSendData.toUtf8());
        mclient->flush();
        qDebug()<<toSendData;
    }
    

    mythread.cpp

    MyThread::MyThread(int ID, QObject *parent) :
        QThread(parent)
    {
    // übergibt die ID
        this->socketDescriptor = ID;
    }
    
    void MyThread::run()
    {
        //thread starts here
        qDebug()<<socketDescriptor<<"Starting thread";
        client = new QTcpSocket();
        if(!client->setSocketDescriptor(this->socketDescriptor))
        {
            emit error(client->error());
            return;
        }
    
        connect(client,SIGNAL(readyRead()),this,SLOT(readyRead()),Qt::DirectConnection);
        connect(client,SIGNAL(disconnected()),this,SLOT(disconnected()),Qt::DirectConnection);
    
        qDebug()<<socketDescriptor<<"Client Connected";
        client->write("mythread is writting");
        exec();
    }
    
    void MyThread::readyRead()
    {
        QByteArray data = client->readAll();
    
        qDebug()<< socketDescriptor << "Data in:"<<data;
    
        client->write("data");
    
    }
    
    void MyThread::disconnected()
    {
        qDebug()<< socketDescriptor << "Disconnected";
        client->deleteLater();
        exit(0);
    }
    
    

    Thank you in advance.

    Regards

    kshegunovK 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      many of us agree that the examples available on the topic are too limited or even downright misleading so we put together another one: https://wiki.qt.io/index.php?title=WIP-How_to_create_a_simple_chat_application that example should help you given it sends "strings" in the form of JSON documents

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      M 1 Reply Last reply
      8
      • VRoninV VRonin

        many of us agree that the examples available on the topic are too limited or even downright misleading so we put together another one: https://wiki.qt.io/index.php?title=WIP-How_to_create_a_simple_chat_application that example should help you given it sends "strings" in the form of JSON documents

        M Offline
        M Offline
        Mogli123
        wrote on last edited by
        #3

        @VRonin
        Thank you
        i will try it

        1 Reply Last reply
        0
        • M Mogli123

          Hi,

          I would to send datas (QString) from a qt-project to another, via qtcpserver.
          I have the code from the following example
          QTcpServer Multithreaded Client
          but I don't know how to send my data to the clients.
          I can only send data once at the beginning when the client is connecting to the server.
          I hope somewone can giv me a tip or a pice of code.

          This is my code.

          mytcpserver.h

          #include <QObject>
          #include <QTcpServer>
          #include <QtDebug>
          #include "mythread.h"
          
          class MyTcpServer : public QTcpServer
          {
              Q_OBJECT
          public:
              explicit MyTcpServer(QObject *parent = nullptr);
              void StartServer();
          
          signals:
              void sendData(QString sendData);
          
          public slots:
              void dataToSend(QString dataToSend);
          
          
          private:
              MyThread *mThread;
          
          protected:
              void incomingConnection(qintptr socketDescriptor);
          
          

          mythread.h

          #include <QObject>
          #include <QThread>
          #include <QTcpSocket>
          #include <QDebug>
          
          class MyThread : public QThread
          {
              Q_OBJECT
          public:
              explicit MyThread(int ID, QObject *parent = nullptr);
              void run();
          
          signals:
              void error(QTcpSocket::SocketError socketerror);
          
          
          public slots:
              void readyRead();
              void disconnected();
          
          private:
              QTcpSocket *client;
              int socketDescriptor;
          
          };
          

          myserver.cpp

          MyTcpServer::MyTcpServer(QObject *parent) :
              QTcpServer(parent)
          {
          }
          
          void MyTcpServer::StartServer()
          {
              if (!this->listen(QHostAddress::Any,5000))
              {
                  qDebug()<< "second Could not start server";
              }
              else
              {
               qDebug()<<"Listening...";
              }
          }
          
          void MyTcpServer::incomingConnection(qintptr socketDescriptor)
          {
              qDebug()<< socketDescriptor<<"Connecting...";
              MyThread *thread = new MyThread(socketDescriptor, this);
              connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
              thread->start();
          }
          
          void MyTcpServer::dataToSend(QString toSendData)
          {
              mclient->waitForReadyRead(300);
          //  schickt den empfangenen String an die Verbundenen Clients
              mclient->write("Pushservice TCP-Client:" + toSendData.toUtf8());
              mclient->flush();
              qDebug()<<toSendData;
          }
          

          mythread.cpp

          MyThread::MyThread(int ID, QObject *parent) :
              QThread(parent)
          {
          // übergibt die ID
              this->socketDescriptor = ID;
          }
          
          void MyThread::run()
          {
              //thread starts here
              qDebug()<<socketDescriptor<<"Starting thread";
              client = new QTcpSocket();
              if(!client->setSocketDescriptor(this->socketDescriptor))
              {
                  emit error(client->error());
                  return;
              }
          
              connect(client,SIGNAL(readyRead()),this,SLOT(readyRead()),Qt::DirectConnection);
              connect(client,SIGNAL(disconnected()),this,SLOT(disconnected()),Qt::DirectConnection);
          
              qDebug()<<socketDescriptor<<"Client Connected";
              client->write("mythread is writting");
              exec();
          }
          
          void MyThread::readyRead()
          {
              QByteArray data = client->readAll();
          
              qDebug()<< socketDescriptor << "Data in:"<<data;
          
              client->write("data");
          
          }
          
          void MyThread::disconnected()
          {
              qDebug()<< socketDescriptor << "Disconnected";
              client->deleteLater();
              exit(0);
          }
          
          

          Thank you in advance.

          Regards

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @Mogli123 said in multi threading send data to clients:

          QTcpServer Multithreaded Client

          This example is bogus, don't use it.
          Here's your hint:

          Note that Qt::DirectConnection is used for the readyRead connection. This is because it's multithreaded, otherwise we may have some operation issues. This makes the slot to be invoked immediately, when the signal is emitted.

          False statement and if you follow the code leading to this paragraph you're going to see this is due to the person who wrote the code has no idea what (s)he's doing. It's a race condition in the socket reading.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          5

          • Login

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