Important: Please read the Qt Code of Conduct -

[Solved] How to react to closed QTcpSocket

  • Dear all,

    I make progess in my skills on how to communicate between sockets. Currently, I have implemented a server, that is inherited by QTcpServer. I react on the signal "new connection" after I start listening mode. Wenn new connection is called, take the nextPendingConnection Socket and store a point to it in a list of all connected sockets. Then I have a nice communication application with allows communication between different client (over the server), too.

    The problem is: Is one of the clients is quit, in my implementation the server doesnt know that. So, if the server start to communicate with this client, I get some errors, as the socket is not connected anymore. I think of two solutions, but dont know how to realize either of them

    1. (dirty)
      When the server start to send data, check is the socket is still connected. How do I do that? QTcpSocket.isOpen() does not seems to do the job, as it results in "true" when the connected socket is closed....

    2. (clean)
      Is there any signal I get catch on the server then a client is closed? Then I could simply clean up my list of "valid" clients. Any suggestions?

    kind regards,

  • In my opinion the best way is using signal disconnected() from client's QTcpSocket.

  • Hiii,
    use following slot for Disconnected signal at server side

    void Server::slotDisconnected()


    QTcpSocket client = (QTcpSocket)sender();//it will point the client which would disconnected


    hope it helps :)

  • Yeah, it really solved the problem. Thanks a lot!

Log in to reply