Y
Y
Yapparov_Ilyas2016-12-10 12:09:23
Qt
Yapparov_Ilyas, 2016-12-10 12:09:23

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);

This is where the error lies. The userId descriptor is always -1 because the socket object itself has already been destroyed.
In this case, how do you know which QMap element to remove? Identify everything via IP and port number?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rou1997, 2016-12-10
@Yapparov_Ilyas

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 question

Ask a Question

731 491 924 answers to any question