Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

multi threading send data to clients



  • 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



  • 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



  • @VRonin
    Thank you
    i will try it


  • Moderators

    @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.


Log in to reply