Answer the question
In order to leave comments, you need to log in
QT: How can the server know which socket has disconnected?
Hello, I am writing a small client-server application in Qt. I am using QTcpServer. All connections are stored in (handle and socket pointer).
When the socket is disconnected, slotDisconnected() is called, which should remove the connection information from the QMap:QMap<int,QTcpSocket*>
int userId= ((QTcpSocket*)sender())->socketDescriptor();
Clients.remove(userId);
Answer the question
In order to leave comments, you need to log in
Delete the one that the return value points to sender()
, you didn't copy the object when you added the pointer to the container, which means it's the same object, I don't know about QMap, but usually lists support deleting an object without a key or index, in a special method you pass a pointer and the list removes that element.
If this is a problem, then yes, implement your own identification.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question