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. how to detect when a server is disconnected
QtWS25 Last Chance

how to detect when a server is disconnected

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 5 Posters 5.2k Views
  • 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.
  • M ManiRon
    14 Mar 2019, 11:52

    @JonB said in how to detect when a server is disconnected:

    that

    actually when i press the stop server button which i have placed in that i will close the server. But what happens when we close the whole application . because when i close the whole application using the windows close button the the connect call is triggered

    J Offline
    J Offline
    JonB
    wrote on 14 Mar 2019, 11:55 last edited by
    #7

    @ManiRon

    i will close the server

    @jsulm & I are asking you precisely what you mean here. What code do you actually execute to "close" the server, you're not telling us?

    M 1 Reply Last reply 14 Mar 2019, 12:03
    0
    • M ManiRon
      14 Mar 2019, 11:52

      @JonB said in how to detect when a server is disconnected:

      that

      actually when i press the stop server button which i have placed in that i will close the server. But what happens when we close the whole application . because when i close the whole application using the windows close button the the connect call is triggered

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 14 Mar 2019, 11:57 last edited by
      #8

      @ManiRon Sorry, but I'm out from this thread as you don't provide any useful information, I'm not going to guess...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 14 Mar 2019, 12:00
      0
      • J jsulm
        14 Mar 2019, 11:52

        @ManiRon Please explain what you mean by "closes the server". What is closed then? What is server in this context? From what you provided so far I really have NO idea what exactly you're doing...

        M Offline
        M Offline
        ManiRon
        wrote on 14 Mar 2019, 11:57 last edited by ManiRon
        #9

        @jsulm 0_1552564450904_Untitled.jpg

        This is how my application looks like . when i click start server button

        "connect(server->tcpServer, &QTcpServer::newConnection, server, &ServerStuff::newConnection);" this i have given

        inside newconnection()

        QTcpSocket *clientSocket = tcpServer->nextPendingConnection();

        connect(clientSocket, &QTcpSocket::disconnected, clientSocket, &QTcpSocket::deleteLater);
        connect(clientSocket, &QTcpSocket::readyRead, this, &ServerStuff::readClient);
        connect(clientSocket, &QTcpSocket::disconnected, this, &ServerStuff::gotDisconnection);
        
        clients << clientSocket;
        

        these are the lines

        smilarly when i press stop server button
        "if(server->tcpServer->isListening())
        {
        disconnect(server->tcpServer, &QTcpServer::newConnection, server, &ServerStuff::newConnection);

            QList<QTcpSocket *> clients = server->getClients();
            server->tcpServer->close();
            ui->textEdit_log->append(tr("<b>Server stopped</b>, post is closed"));
        }"
        

        this is what is the code

        ServerStuff(QObject *pwgt);
        QTcpServer *tcpServer;
        QList<QTcpSocket *> getClients();

        J 1 Reply Last reply 15 Mar 2019, 08:43
        0
        • J jsulm
          14 Mar 2019, 11:57

          @ManiRon Sorry, but I'm out from this thread as you don't provide any useful information, I'm not going to guess...

          M Offline
          M Offline
          ManiRon
          wrote on 14 Mar 2019, 12:00 last edited by
          #10

          @jsulm sir i have provided

          J 1 Reply Last reply 14 Mar 2019, 12:02
          0
          • M ManiRon
            14 Mar 2019, 12:00

            @jsulm sir i have provided

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 14 Mar 2019, 12:02 last edited by
            #11

            @ManiRon You don't disconnect the clients when stopping server

            for (QTcpSocket *socket : sockets)
                socket->deleteLater();
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • J JonB
              14 Mar 2019, 11:55

              @ManiRon

              i will close the server

              @jsulm & I are asking you precisely what you mean here. What code do you actually execute to "close" the server, you're not telling us?

              M Offline
              M Offline
              ManiRon
              wrote on 14 Mar 2019, 12:03 last edited by
              #12

              @JonB @jsulm now my doubt is when i press the stop server button with the above mentioned code inside it doesnt trigger the connect in client but when i close the whole application by using the windows (x) button then it triggers the connect in client

              J 1 Reply Last reply 14 Mar 2019, 12:04
              0
              • M ManiRon
                14 Mar 2019, 12:03

                @JonB @jsulm now my doubt is when i press the stop server button with the above mentioned code inside it doesnt trigger the connect in client but when i close the whole application by using the windows (x) button then it triggers the connect in client

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 14 Mar 2019, 12:04 last edited by
                #13

                @ManiRon I guess you mean it triggers disconnect, not connect?
                Did you try what I suggested?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                M 1 Reply Last reply 15 Mar 2019, 04:31
                0
                • J jsulm
                  14 Mar 2019, 12:04

                  @ManiRon I guess you mean it triggers disconnect, not connect?
                  Did you try what I suggested?

                  M Offline
                  M Offline
                  ManiRon
                  wrote on 15 Mar 2019, 04:31 last edited by
                  #14

                  @jsulm yes i tried but still the same , No actually i have made a connect in client side when ever the server side disconnects it will trigger my function in which i will print disconnected . this is triggered when i close the whole application but when i press that stop server button with what you have said it didnt work.

                  if(server->tcpServer->isListening())
                  {
                  disconnect(server->tcpServer, &QTcpServer::newConnection, server, &ServerStuff::newConnection);

                      QList<QTcpSocket *> clients = server->getClients();
                      for(int i = 0; i < clients.count(); i++)
                      {
                          //server->sendToClient(clients.at(i), "Connection closed");
                  

                  // server->sendToClient(clients.at(i), "0");
                  }

                      //server->tcpServer->close();
                      server->clientSocket->deleteLater();
                      //server->tcpServer->close();
                      ui->textEdit_log->append(tr("<b>Server stopped</b>, post is closed"));
                  }
                  
                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 15 Mar 2019, 08:03 last edited by
                    #15

                    You should take a look at

                    • QAbstractSocket::disconnectFromHost()
                    • QAbstractSocket::waitForDisconnected()

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    J 1 Reply Last reply 15 Mar 2019, 08:06
                    1
                    • C Christian Ehrlicher
                      15 Mar 2019, 08:03

                      You should take a look at

                      • QAbstractSocket::disconnectFromHost()
                      • QAbstractSocket::waitForDisconnected()
                      J Offline
                      J Offline
                      JonB
                      wrote on 15 Mar 2019, 08:06 last edited by
                      #16

                      @Christian-Ehrlicher

                      QAbstractSocket::disconnectFromHost()

                      At last someone points to a useful function! So does this mean if closing at server side it is its job to iterate each client and issue that call? There is not an automatic "shut me down and I'll disconnect from each client for you"? Just trying to understand.

                      1 Reply Last reply
                      0
                      • M ManiRon
                        14 Mar 2019, 11:57

                        @jsulm 0_1552564450904_Untitled.jpg

                        This is how my application looks like . when i click start server button

                        "connect(server->tcpServer, &QTcpServer::newConnection, server, &ServerStuff::newConnection);" this i have given

                        inside newconnection()

                        QTcpSocket *clientSocket = tcpServer->nextPendingConnection();

                        connect(clientSocket, &QTcpSocket::disconnected, clientSocket, &QTcpSocket::deleteLater);
                        connect(clientSocket, &QTcpSocket::readyRead, this, &ServerStuff::readClient);
                        connect(clientSocket, &QTcpSocket::disconnected, this, &ServerStuff::gotDisconnection);
                        
                        clients << clientSocket;
                        

                        these are the lines

                        smilarly when i press stop server button
                        "if(server->tcpServer->isListening())
                        {
                        disconnect(server->tcpServer, &QTcpServer::newConnection, server, &ServerStuff::newConnection);

                            QList<QTcpSocket *> clients = server->getClients();
                            server->tcpServer->close();
                            ui->textEdit_log->append(tr("<b>Server stopped</b>, post is closed"));
                        }"
                        

                        this is what is the code

                        ServerStuff(QObject *pwgt);
                        QTcpServer *tcpServer;
                        QList<QTcpSocket *> getClients();

                        J Offline
                        J Offline
                        J.Hilk
                        Moderators
                        wrote on 15 Mar 2019, 08:43 last edited by J.Hilk
                        #17

                        @ManiRon
                        if I get this, right, than pressing stop server, behaves as expected but closing the window and therefore closing/quitting your app does not follow the correct server shotdown procedure?

                        In that case I would suggest overriding the close event something along this line:

                        //My assumption is your Window class is named MainWindow, if not you'll have to adapt that
                        
                        //inside the header
                        protected:
                              virtual void closeEvent (QCloseEvent *event) override;
                        
                        //inside your cpp file
                        
                         #include <QCloseEvent>
                        void MainWindow::closeEvent (QCloseEvent *event)
                        {
                            sameFunctionWhenStopServerClicked();
                                
                            event->accept();
                        }
                        

                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        J 1 Reply Last reply 15 Mar 2019, 08:46
                        0
                        • J J.Hilk
                          15 Mar 2019, 08:43

                          @ManiRon
                          if I get this, right, than pressing stop server, behaves as expected but closing the window and therefore closing/quitting your app does not follow the correct server shotdown procedure?

                          In that case I would suggest overriding the close event something along this line:

                          //My assumption is your Window class is named MainWindow, if not you'll have to adapt that
                          
                          //inside the header
                          protected:
                                virtual void closeEvent (QCloseEvent *event) override;
                          
                          //inside your cpp file
                          
                           #include <QCloseEvent>
                          void MainWindow::closeEvent (QCloseEvent *event)
                          {
                              sameFunctionWhenStopServerClicked();
                                  
                              event->accept();
                          }
                          
                          J Offline
                          J Offline
                          JonB
                          wrote on 15 Mar 2019, 08:46 last edited by JonB
                          #18

                          @J.Hilk
                          I'm afraid you're the wrong way round! For OP closing app does correctly disconnect, his problem is that he does not know exactly what code to write to make it work correctly if he does not close but wants his own code (off a button press) to do the socket closure such that client sees it.... For which @Christian-Ehrlicher has told him he will need to call QAbstractSocket::disconnectFromHost(), and I am trying to verify with him this must be done explcitly on all connected client sockets.

                          J 1 Reply Last reply 15 Mar 2019, 08:51
                          1
                          • J JonB
                            15 Mar 2019, 08:46

                            @J.Hilk
                            I'm afraid you're the wrong way round! For OP closing app does correctly disconnect, his problem is that he does not know exactly what code to write to make it work correctly if he does not close but wants his own code (off a button press) to do the socket closure such that client sees it.... For which @Christian-Ehrlicher has told him he will need to call QAbstractSocket::disconnectFromHost(), and I am trying to verify with him this must be done explcitly on all connected client sockets.

                            J Offline
                            J Offline
                            J.Hilk
                            Moderators
                            wrote on 15 Mar 2019, 08:51 last edited by
                            #19

                            @JonB well in that case my answer isn't of much help ;-)


                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            J 1 Reply Last reply 15 Mar 2019, 08:54
                            0
                            • J J.Hilk
                              15 Mar 2019, 08:51

                              @JonB well in that case my answer isn't of much help ;-)

                              J Offline
                              J Offline
                              JonB
                              wrote on 15 Mar 2019, 08:54 last edited by
                              #20

                              @J.Hilk Well, it was a nice answer to a different question :)

                              1 Reply Last reply
                              0

                              16/20

                              15 Mar 2019, 08:06

                              • Login

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