Solved QTcpServer/Socket remain connected while client dies
This is "fun"... I have client, server test app, and when I terminate either client. The server side tcpSocket does not die/notify me of disconnection.
When I check the socket.state() I always get ConnectedState. Even 5 min after I killed other app& socket. How can I "properly" test if the socket is really connected? Or how can I configure timeout for socket ?
I have done this to my socket when I accepted the connection
clientConnection = self.socketServer.nextPendingConnection() socket = QTcpSocket() socket.setSocketOption(QAbstractSocket.KeepAliveOption, 1) # apparently has to be done before connection was made on it for windows... socket.setSocketDescriptor(clientConnection.socketDescriptor()) socket.setSocketOption(QAbstractSocket.KeepAliveOption, 1) # just to be sure... self.clientList.append(socket)
That socket never tells me it dies and always has connectedState... sigh.
For some reason one of my QThread workers send a message on socket and did not post it to Socket thread... that caused it to internally "crash" without telling any1 about it and thus became "broken" internally. Once I posted it to correct thread everything started to work again... sigh! :- )