In my opinion the best approach is to use proper encapsulation. If you have a class for handling client connections, you'll end up with a situation where one socket is bound with exactly one instance of the connection class. Therefore you immediately know which client you are handling because each handler object only deals with one socket.
Q_OBJECT
public:
connect(sock, SIGNAL(readyRead()), this, SLOT(handleInput()));
}
private slots:
void handleInput() {
doStuffWith(data);
}
private:
};
class Handler : public QObject {
Q_OBJECT
public:
Handler(QTcpSocket *clientSocket, QObject *parent = 0) : QObject(parent), sock(clientSocket) {
connect(sock, SIGNAL(readyRead()), this, SLOT(handleInput()));
}
private slots:
void handleInput() {
QByteArray data = sock->readAll();
doStuffWith(data);
}
private:
QTcpSocket *sock;
};
To copy to clipboard, switch view to plain text mode
Bookmarks