⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

QTcpServer and QTcpSocker

  • Hi all,

    I have a client server application, with two tcpsockets on each side. My problem is notification problem when the server disconnect its socket.

    Disconnection Part 1: When the client socket disconnects from the server, I get a stateChanged signal of the server socket. That works well.

    void SocketServer::onStateChanged( QAbstractSocket::SocketState state )
        switch( state )
            case QAbstractSocket::UnconnectedState:
                if( m_socket )
                    qDebug( "Socket disconnected" );

    Disconnection part 2: After a timeout of 5 minutes, the server will close its socket due to timeout (don't ask why...). I use a similar function as above, but I don't receive a stateChanged signal.

    Does anybody know why?


  • For a better understanding, I told the server to close the connection after that timeout.

  • Ok, additional information:
    My client is a QThread. It works when I used the client unthreaded, but not as threaded.

    Any ideas?

  • I found the solution:
    The socket is setup within a thread and it's waiting / blocked by QWaitCondition until a message is send. When a message is send, the thread wakes up, the socket sends the message and waits until readyRead or timeout is triggered. That's working, but after that the thread is going to wait again and the socket is blocked again, so this is why the disconnect signal didn't get through.

    Now I replaced the QWaitCondition by waitForDisconnect() and so the socket is not blocked anymore by the wait condition, and the disconnect signal gets through.

    I think this is solved.

Log in to reply