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. How to send message to all client?

How to send message to all client?

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

    Hey, I'm new member here.

    I want to create Chat Application with Qt in C++. My client application is done but my tcp server is not working very well! I can't send message to diferent client. You can help me?

    Here is my TCP server

    #include "myserver.h"
    #include "mythread.h"
    
    MyServer::MyServer(QObject *parent): QTcpServer(parent){
        qDebug() << "Server -> Invited Constructor";
        ClientList.clear();
    }
    MyServer::~MyServer(){
        qDebug() << "Server -> Invited Destructor";
    }
    
    void MyServer::StartServer(){
        if(!this->listen(QHostAddress::Any,SERVER_PORT)){
            qDebug() << QObject::tr("Unable to start the server: %1.").arg(tcpServer->errorString());
            //this->deleteLater();
            return;
        }else{
            qDebug() << QString::fromUtf8("Server is running");
        }
    }
    
    
    void MyServer::incomingConnection(qintptr socketDescriptor)
    {
        printf("MyServer::incomingConnection\n");
        qDebug() << socketDescriptor << " Thread is Connecting...";
        MyThread *thread = new MyThread(socketDescriptor,SERVER_PORT, this);
        connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
        thread->start();
    }
    
    

    Here is my Thread:

    #include "mythread.h"
    
    
    MyThread::MyThread(qintptr ID,quint16 port, QObject * parent) : QThread(parent)
    {
        this->socketDescriptor = ID;
        SERVER_PORT = port;
    }
    
    
    void MyThread::run(){
        //thread starts here
        qDebug()<< socketDescriptor << "Starting thread on port: "<<SERVER_PORT;
    
        tcpServer = new QTcpServer(this);
    
        socket = new QTcpSocket(this);
        // set the ID
        if(!socket->setSocketDescriptor(this->socketDescriptor)){
            emit error(socket->error());
            return;
        }
    
        connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()),Qt::DirectConnection);
        connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()),Qt::DirectConnection);
    
        qDebug()<< socketDescriptor <<"Client Connected";
    
        exec(); 
    }
    
    void MyThread::readyRead(){
        QByteArray Data = socket->readAll();
        qDebug() << socketDescriptor <<"Data in : "<< Data;
        socket ->write(Data); //its work, but i want to send for all connected client!
    }
    
    void MyThread::disconnected(){
        qDebug() << socketDescriptor << "Disconnected: ";
        socket->deleteLater();
        exit(0);
    }
    
    

    My solution:

    myserver.cpp

    void MyServer::incomingConnection(int socketDescription){
    
        qDebug() << socketDescription << "Connecting";
        qDebug() <<this->socketDescriptor() << "this is the socket Descriptor";
       //new client add to QList
        QTcpSocket *socket = this->nextPendingConnection();
        ClientList.append(socket);
        qDebug() << socket << "This will be added to list";
        ClientList.append(socket);
        MyThread * thread = new MyThread(socketDescription,ClientList,this);
        connect(thread, SIGNAL(finished()),thread,SLOT(deleLater()));
        thread ->start();
    }
    

    mythread.cpp

    void MyThread::readyRead(){
        QByteArray Data = socket->readAll();
        qDebug() << socketDescriptor <<"Data in : "<< Data;
        //socket ->write(Data);     
        foreach(QTcpSocket *clientConnection, threadClientList) {
                               clientConnection->write(Data);
                               qDebug() <<"MyThred -> ClientConnection: " << clientConnection;
                        }
    
    }
    

    but this solution dont work!

    sorry for bad language,
    Krizbi

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Don't use threads at all, remember all socket connections in a container and iterate over this list when you want to send to all clients.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • K Offline
        K Offline
        Krizbiii
        wrote on last edited by
        #3

        you can help me with some code?

        aha_1980A 1 Reply Last reply
        0
        • K Krizbiii

          you can help me with some code?

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Krizbiii

          You could have a look at the server part of: https://wiki.qt.io/WIP-How_to_create_a_simple_chat_application#The_Single_Thread_Server especially the function ChatServer::broadcast() which does what you want to do.

          Regards

          Qt has to stay free or it will die.

          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