TCP Socket question [windows]

  • Hi,
    I'm calling this method every 5 seconds in my app to try to detect a deconnection from TCP Server. But this is not reliable every time... Sometimes it works sometimes not. Right now my server is Off for 5 minutes but the method still returns true

    bool Qwd::isConnectedToServer()
        if(sk.state() == QTcpSocket::ConnectedState)
            return true;
            return false;

    How to detect that the server is turned off please ?

    can you implement in your sk a function to read QAbstractSocket::disconnected()?? like that you will know when you will be disconnected from the server
    2) do something like a ping - send to the server a command and wait for the server to reply back.

  • @arsinte_andrei hi
    this lambdas are entered if i shut down my client, but if i turn off the server nothing happends

      QObject::connect(&sk, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
                         [=](QAbstractSocket::SocketError err){
           qDebug()<<"DISCONNECTED ! ";
             qDebug()<<"DISCONNECTED ! ";

  • then it remains only to check by pinging the server...
    basically, just send a 4-byte thing to the server and make the server to send back that as a reply... or something like that and if you get the reply then you know that the server is alive...

    but this method will overkill your server if it will have many services connected to it...

    what you can do is to write the program in such a way that after it sends something to the server and the server will respond back.. if the server didn't respond back is off so it did not get the message... so check after every single sent message if the server responded and do not send a new message unless it responded for the preview one...

  • @arsinte_andrei thx
    I will implement ping/heartbeat mechanism as you suggested

  • @LeLev
    is the only way to see that the server is alive - even in http server the server response back after each request... so you have to implement something like that if what you need is a crucial connection...but to avoid overloading of the server as I've said - just check for a response back from the server with a time delay... so use a qtimer and after the time has passed - few second there just say that the server is off - same like in http protocol - something like your network might be down or the server too busy to respond or,... you will find your one...

  • @LeLev
    I had a similiar problem in a project.
    To detect if the server accepted the connection or refused, I observed the connectionState.

    // a part of my code
     connect(_socket, &QTcpSocket::stateChanged, this, &MyClient::socketStateChanged);
    // detection
    void MyClient::socketStateChanged(QAbstractSocket::SocketState state)
        if(state == QTcpSocket::ConnectedState){
            qDebug() << QString("%1:%2").arg(_socket->peerAddress().toString()).arg(_socket->peerPort()) << " Connected";
            disconnect(_socket, &QTcpSocket::stateChanged, this, &MyClient::socketStateChanged);
            emit connected();
        else if(state == QAbstractSocket::UnconnectedState){
            qDebug() << "Connection Refused";
            disconnect(_socket, &QTcpSocket::stateChanged, this, &MyClient::socketStateChanged);
            emit disconnected();

    After to check if the connection has been established, i have removed the connection to just be notified by socket::disconnected signal

Log in to reply