Search code examples
c++qttcpqlist

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!


Solution

  • The solution:

    void Server::readyRead(){
    
        QByteArray buffer;
        QTcpSocket* readSocket = qobject_cast<QTcpSocket*>(sender());
        buffer = readSocket->readAll();
    
        QString mytext = QString::fromStdString(buffer);
        qDebug() << mytext;
    }