I’ve created a hello-world program to help me understand how QTcpServer and QTcpSocket work. In it the QTcpServer accepts new connections and connects their readyRead() signal to the MainWindow::onNewData() slot where their new data is appended to a plain text control.
The problem is that multiple sockets can be opened simultaneously. So when I get the readyRead() signal and I want to read the data with readAll(), how do I know which socket emitted it?
Here’s the relevant parts of the code. I’m not storing the QTcpSocket pointers at all here, but even if I did, I still wouldn’t know which one of them emitted the signal.
// constructor
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
server->listen(QHostAddress::LocalHost, 3333);
void MainWindow::onNewConnection()
{
QTcpSocket* socket = server->nextPendingConnection();
connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
connect(socket, SIGNAL(readyRead()), this, SLOT(onNewData()));
}
void MainWindow::onNewData()
{
ui->plainTextEdit->appendPlainText(WHICH_SOCKET_EMITTED_IT()->readAll());
}
The best way (as it seems to me) is to implement your own
Clientclass which will encapsulateQTcpSocketEasiest way: use
QObject::sender()in slot function