QTcpSocket state always connected



  • Hi,
    I have a problem with the status of the tcp socket. If the server disconnects or I unplug the ethernet cable, the status remains "Connected".
    Do I have to set something beyond the normal creation of the socket?
    Does the KeepAliveOption have something to do with it?

    Thanks


  • Moderators

    @Gianluca86
    a little bit more of information would be helpful.
    What OS, Qt version, etc.



  • I'm using Qt 5.5 on Ubuntu 32 Bit.
    Unfortunately I can't upgrade to 64 Bit and 5.9.
    This a little example

    // Open
    m_pTcpSocket = new QTcpSocket();
    m_pTcpSocket->connectToHost(strIP,uiPort);
    if(!m_pTcpSocket->waitForConnected(lTimeout))
    {// Error
       Close();
    }
    
    // Check
    if (m_pTcpSocket->state() == QAbstractSocket::ConnectedState)
    {
       return bConnected;
    }
    

  • Moderators

    @Gianluca86
    and how are you checking if you stay connected?
    I mean the condition which leads you to "stays always connected".



  • Sorry, I forgot the else:

    // Check
    if (m_pTcpSocket->state() == QAbstractSocket::ConnectedState)
    {
       return bConnected;
    }
    else
    {// Do other stuff
    }
    

    and I insert before the If

    qDebug()<<m_pTcpSocket->state();
    


  • add QObject::connect(m_pTcpSocket,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected");});

    and see if it prints the disconnection message


  • Moderators

    @Gianluca86 said in QTcpSocket state always connected:

    If the server disconnects or I unplug the ethernet cable, the status remains "Connected".

    i still don't see any code where you would (dynamically) react on such event?!
    Or where do you assume in your code that to happen?



  • @raven-worx :
    I didn't write all the code, however I always call the function that contains the check if, what I wrote before.

    @VRonin :
    I can't, I have a error.


  • Moderators

    @Gianluca86 said in QTcpSocket state always connected:

    @raven-worx :
    I didn't write all the code, however I always call the function that contains the check if, what I wrote before.

    @VRonin :
    I can't, I have a error.

    Do you really expect help with such horrible replies?
    You simply hide code, you just say you have an error, but dont even tell with one .... seriously?



  • @Gianluca86 said in QTcpSocket state always connected:

    I can't, I have a error.

    What kind of error?



  • @raven-worx :
    The error does not depend on my code.
    It is simply the Socket that returns the Connected state.
    As I said, I call the IF, nothing else. There is no other code I need to show.

    @VRonin :
    warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]
    QObject::connect(m_pTcpSocket,&QAbstractSocket::disconnected,->void{qDebug("Disconnected");});
    ^



  • @Gianluca86 said in QTcpSocket state always connected:

    lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]

    add CONFIG += c++11 to your .pro file and re-run qmake to fix this or just create a Q_SLOT void logDisconnect(){qDebug("Disconnected");} and connect to that instead of the lambda



  • It prints the disconnection message. But the state doesn't change.
    I think I'll give up, I'll use the Send and Receive timeout to check the connection. Thanks for the help.


  • Moderators

    @Gianluca86 said in QTcpSocket state always connected:

    It prints the disconnection message. But the state doesn't change.

    Since you said you do not need to post more code, i tell you it's impossible to achieve what you want with the code you've posted so far.
    I also do not know how you are checking the state. Obviously the code you've posted only checks the state once. And thats right after a connection happened...

    So what do you actually expect?



  • After the connection, I call the function with the check state every 3 seconds, with a timer.
    I don't do anything special, I simply create a Socket, connect and check state, nothing more.
    So I would expect that if the server program to which I was connected closes, the state of my tcpsocket changes, but unfortunately it doesn't happen, so I'll have to use another method to know if the connection is active.

    thanks anyway



  • @Gianluca86 said in QTcpSocket state always connected:

    But the state doesn't change.

    Pretty sure it does https://code.woboq.org/qt5/qtbase/src/network/socket/qabstractsocket.cpp.html#2788

    In fact you can connect to stateChanged signal to check that


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.