Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. TCP Socket question [windows]
Forum Updated to NodeBB v4.3 + New Features

TCP Socket question [windows]

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 701 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    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;
        else
            return false;
    }
    

    How to detect that the server is turned off please ?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      arsinte_andrei
      wrote on last edited by
      #2

      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.

      ODБOïO 1 Reply Last reply
      0
      • A arsinte_andrei

        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.

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by ODБOï
        #3

        @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 ! ";
            });
        
        // 
            QObject::connect(&sk,&QTcpSocket::disconnected,[](){
                 qDebug()<<"DISCONNECTED ! ";
            });
        
        1 Reply Last reply
        0
        • A Offline
          A Offline
          arsinte_andrei
          wrote on last edited by
          #4

          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...

          1 Reply Last reply
          1
          • ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by
            #5

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

            A KillerSmathK 2 Replies Last reply
            0
            • ODБOïO ODБOï

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

              A Offline
              A Offline
              arsinte_andrei
              wrote on last edited by
              #6

              @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...

              1 Reply Last reply
              1
              • ODБOïO ODБOï

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

                KillerSmathK Offline
                KillerSmathK Offline
                KillerSmath
                wrote on last edited by
                #7

                @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

                @Computer Science Student - Brazil
                Web Developer and Researcher
                “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved