C++ Multi-Client TCP Server with QList



  • Necessary informations:

      QList<QTcpSocket*> list;
      QTcpServer* server;
      QTcpSocket* socket;
    

    In Qt I have built a TCP-Server(QTcpServer)! I have a **QList **with all my connected clients and I want to read the incomming data for each client personally. So if the **QTcpServer **gets a new connection, I handel it like this:

    void Server::newConnection()
    {
       qDebug() << "New Connection";
       list.append(server->nextPendingConnection());
       connect(list.last(),SIGNAL(readyRead()),this,SLOT(readyRead()));
    }
    

    How can I get the correct client (out of my QList), which send the **SIGNAL **readyRead(), in my **SLOT **readyRead()?

    void Server::readyRead(){
           //??
    }
    

    Any help is welcomed!


  • Lifetime Qt Champion

    hi and welcome
    In the slot, you can call sender() to know who sent the signal.
    Its returns as QObject so you must cast it.

    QTcpSocket* soc = qobject_cast<QTcpSocket*>(sender());
    if(soc) ...

    Remember to check that pointer is not NULL.



  • @mrjj Thank you a lot, it works!!!