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. Is there a way to check availability local server with timeout in Qt?
QtWS25 Last Chance

Is there a way to check availability local server with timeout in Qt?

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 4 Posters 1.3k 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.
  • T Offline
    T Offline
    TomNow99
    wrote on 16 Jul 2020, 18:18 last edited by TomNow99
    #1

    Hello,

    I have on my VirtualBox linux with samba server. I have connection only between server and pc with virtualBox ( server can't connect with internet ). Is there a way to check availability server and after 5 seconds say: "no"?

    Something like:

    checkAvailabilityServer("\\192.168.43.21", 5);
    

    ?

    If no Qt, maybe Winapi c++?

    1 Reply Last reply
    1
    • H Offline
      H Offline
      hskoglund
      wrote on 16 Jul 2020, 18:36 last edited by
      #2

      curl to the rescue:

      curl -m 5 smb://192.168.43.21/ShareName/CanaryFile.txt
      

      If the SMB server is alive, curl l will return curl: (67) Login denied
      If the SMB server is down, curl will return curl: (28) Connection timed out after 5000 milliseconds

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TomNow99
        wrote on 16 Jul 2020, 18:37 last edited by TomNow99
        #3

        @hskoglund Thank you. But I don't give one more important information: my host OS is Windows 10. Curl is linux command right?

        C 1 Reply Last reply 16 Jul 2020, 18:46
        0
        • C Offline
          C Offline
          Cobra91151
          wrote on 16 Jul 2020, 18:38 last edited by
          #4

          Hello!

          You can try to use QTcpSocket class. Check out the connectToHost method: (https://doc.qt.io/qt-5/qabstractsocket.html#connectToHost) . Also, feel free to check the waitForConnected method as well: https://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected

          Example:

          bool checkServerConnection() {
              QTcpSocket connectionSocket;
              connectionSocket.connectToHost("192.168.43.21", 5);
              connectionSocket.waitForConnected(5000); // 5 seconnds
          
              if (connectionSocket.state() == QTcpSocket::UnconnectedState) {
                  connectionSocket.close();
                  return false;
              }
          
              connectionSocket.close();
              return true;
          }
          

          Happy coding!

          1 Reply Last reply
          2
          • T Offline
            T Offline
            TomNow99
            wrote on 16 Jul 2020, 18:39 last edited by
            #5

            @Cobra91151 Thank you for answer. Tommorow I check it! :)

            1 Reply Last reply
            0
            • T TomNow99
              16 Jul 2020, 18:37

              @hskoglund Thank you. But I don't give one more important information: my host OS is Windows 10. Curl is linux command right?

              C Offline
              C Offline
              Cobra91151
              wrote on 16 Jul 2020, 18:46 last edited by
              #6

              @TomNow99 said in Is there a way to check availability local server with timeout in Qt?:

              @hskoglund Thank you. But I don't give one more important information: my host OS is Windows 10. Curl is linux command right?

              It is available for Windows OS as well. You can check out this link for more details: https://curl.haxx.se/download.html

              1 Reply Last reply
              1
              • T Offline
                T Offline
                TomNow99
                wrote on 17 Jul 2020, 07:13 last edited by
                #7

                @Cobra91151 @mrjj That QTcpSocket is very good. But what if I have situation that I don't want wait?

                So I want only do:

                QTcpSocket connectionSocket;
                connectionSocket.connectToHost("192.168.43.21", port);
                qDebug()<<connectionSocket.state();
                

                I can no wait and always get "connected" when server is up and "notConnected" when server is down? This is only server on my virtualBox.

                J 1 Reply Last reply 17 Jul 2020, 07:15
                0
                • T TomNow99
                  17 Jul 2020, 07:13

                  @Cobra91151 @mrjj That QTcpSocket is very good. But what if I have situation that I don't want wait?

                  So I want only do:

                  QTcpSocket connectionSocket;
                  connectionSocket.connectToHost("192.168.43.21", port);
                  qDebug()<<connectionSocket.state();
                  

                  I can no wait and always get "connected" when server is up and "notConnected" when server is down? This is only server on my virtualBox.

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 17 Jul 2020, 07:15 last edited by
                  #8

                  @TomNow99 Then use https://doc.qt.io/qt-5/qabstractsocket.html#connected

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    TomNow99
                    wrote on 17 Jul 2020, 07:18 last edited by TomNow99
                    #9

                    @jsulm Thank you. But I have other problem: I want check when server is down and I can't connect. Something like:

                    QTcpSocket connectionSocket;
                    connect(connectionSocket, SIGNAL(cannotConnect()), this, SLOT(cannotConnectSlot()));
                    connectionSocket.connectToHost("192.168.43.21", port);
                    
                    J 1 Reply Last reply 17 Jul 2020, 07:22
                    0
                    • T TomNow99
                      17 Jul 2020, 07:18

                      @jsulm Thank you. But I have other problem: I want check when server is down and I can't connect. Something like:

                      QTcpSocket connectionSocket;
                      connect(connectionSocket, SIGNAL(cannotConnect()), this, SLOT(cannotConnectSlot()));
                      connectionSocket.connectToHost("192.168.43.21", port);
                      
                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 17 Jul 2020, 07:22 last edited by
                      #10

                      @TomNow99 You could use a QTimer with a timeout and if you do not get connected() until timeout you can assume that you can't connect.

                      1 Reply Last reply
                      1
                      • T Offline
                        T Offline
                        TomNow99
                        wrote on 17 Jul 2020, 07:27 last edited by TomNow99
                        #11

                        @jsulm I think about that, but I have many Qtimers :D

                        EDIT:

                        I want something like:

                        Run my server. And in my application I want check connect to this server every 3 seconds. If I can't connect I want write qDebug()<<"noConnection" and end checking.

                        J 1 Reply Last reply 17 Jul 2020, 07:32
                        0
                        • T TomNow99
                          17 Jul 2020, 07:27

                          @jsulm I think about that, but I have many Qtimers :D

                          EDIT:

                          I want something like:

                          Run my server. And in my application I want check connect to this server every 3 seconds. If I can't connect I want write qDebug()<<"noConnection" and end checking.

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 17 Jul 2020, 07:32 last edited by
                          #12

                          @TomNow99 I don't see any other solution as even a working connection needs some time to be established, you can't simply do the qDebug just after calling connectToHost()

                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            TomNow99
                            wrote on 17 Jul 2020, 07:37 last edited by TomNow99
                            #13

                            @jsulm Hmmm. I tried add new Qthread and check connection in this Qthread, but I get error "Cannot create children for a parent that is in a different thread". My code:

                            myThreadClass::myThreadClass(QTcpSocket *connSocket): connectionSocket(connSocket)
                            {
                            
                            }
                            
                            myThreadClass::~myThreadClass()
                            {
                                connectionSocket->close();
                            }
                            
                            void myThreadClass::run()
                            {
                                while(1)
                                {
                                    connectionSocket->connectToHost("192.168.43.23", 139);
                                    connectionSocket->waitForConnected(3000);
                            
                                    if(connectionSocket->state() == QTcpSocket::UnconnectedState)
                                    {
                                        connectionSocket->close();
                                        emit noConnection();
                                    }
                            
                                    connectionSocket->close();
                                }
                            }
                            

                            In mainWindow i create myThread and started it. I add QTcpSocket pointer and get this error.

                            class myThreadClass: public QThread
                            {
                                Q_OBJECT
                            public:
                                myThreadClass(QTcpSocket *connSocket);
                                ~myThreadClass();
                                void run();
                            
                            private:
                                QTcpSocket *connectionSocket;
                            
                            signals:
                                void noConnection();
                            };
                            
                            J 2 Replies Last reply 17 Jul 2020, 07:43
                            0
                            • T TomNow99
                              17 Jul 2020, 07:37

                              @jsulm Hmmm. I tried add new Qthread and check connection in this Qthread, but I get error "Cannot create children for a parent that is in a different thread". My code:

                              myThreadClass::myThreadClass(QTcpSocket *connSocket): connectionSocket(connSocket)
                              {
                              
                              }
                              
                              myThreadClass::~myThreadClass()
                              {
                                  connectionSocket->close();
                              }
                              
                              void myThreadClass::run()
                              {
                                  while(1)
                                  {
                                      connectionSocket->connectToHost("192.168.43.23", 139);
                                      connectionSocket->waitForConnected(3000);
                              
                                      if(connectionSocket->state() == QTcpSocket::UnconnectedState)
                                      {
                                          connectionSocket->close();
                                          emit noConnection();
                                      }
                              
                                      connectionSocket->close();
                                  }
                              }
                              

                              In mainWindow i create myThread and started it. I add QTcpSocket pointer and get this error.

                              class myThreadClass: public QThread
                              {
                                  Q_OBJECT
                              public:
                                  myThreadClass(QTcpSocket *connSocket);
                                  ~myThreadClass();
                                  void run();
                              
                              private:
                                  QTcpSocket *connectionSocket;
                              
                              signals:
                                  void noConnection();
                              };
                              
                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 17 Jul 2020, 07:43 last edited by
                              #14
                              This post is deleted!
                              1 Reply Last reply
                              0
                              • T TomNow99
                                17 Jul 2020, 07:37

                                @jsulm Hmmm. I tried add new Qthread and check connection in this Qthread, but I get error "Cannot create children for a parent that is in a different thread". My code:

                                myThreadClass::myThreadClass(QTcpSocket *connSocket): connectionSocket(connSocket)
                                {
                                
                                }
                                
                                myThreadClass::~myThreadClass()
                                {
                                    connectionSocket->close();
                                }
                                
                                void myThreadClass::run()
                                {
                                    while(1)
                                    {
                                        connectionSocket->connectToHost("192.168.43.23", 139);
                                        connectionSocket->waitForConnected(3000);
                                
                                        if(connectionSocket->state() == QTcpSocket::UnconnectedState)
                                        {
                                            connectionSocket->close();
                                            emit noConnection();
                                        }
                                
                                        connectionSocket->close();
                                    }
                                }
                                

                                In mainWindow i create myThread and started it. I add QTcpSocket pointer and get this error.

                                class myThreadClass: public QThread
                                {
                                    Q_OBJECT
                                public:
                                    myThreadClass(QTcpSocket *connSocket);
                                    ~myThreadClass();
                                    void run();
                                
                                private:
                                    QTcpSocket *connectionSocket;
                                
                                signals:
                                    void noConnection();
                                };
                                
                                J Offline
                                J Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 17 Jul 2020, 07:45 last edited by
                                #15

                                @TomNow99 said in Is there a way to check availability local server with timeout in Qt?:

                                while(1)

                                You're blocking event loop...
                                And the socket must be allocated in the thread where you want to use it.

                                1 Reply Last reply
                                0
                                • T Offline
                                  T Offline
                                  TomNow99
                                  wrote on 17 Jul 2020, 07:46 last edited by
                                  #16

                                  @jsulm I would like to check still connection that I add while(1)

                                  J 1 Reply Last reply 17 Jul 2020, 07:48
                                  0
                                  • T TomNow99
                                    17 Jul 2020, 07:46

                                    @jsulm I would like to check still connection that I add while(1)

                                    J Offline
                                    J Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on 17 Jul 2020, 07:48 last edited by
                                    #17

                                    @TomNow99 I'm still not sure why you want this complicated solution using a thread if you can have one using a timer...

                                    1 Reply Last reply
                                    0
                                    • T Offline
                                      T Offline
                                      TomNow99
                                      wrote on 17 Jul 2020, 07:53 last edited by TomNow99
                                      #18

                                      @jsulm Hmmm, but how? Something like:

                                      in QTimerSlot ( check evert 3 sec ) I have:

                                      connectionSocket->connectToHost("192.168.43.23", 139);
                                      timer2.start(1000); // maybe singleShot
                                      
                                      

                                      and how to connect information from slotConnected and other QTimer?

                                      slotConnected()
                                      {
                                          result = true;
                                      }
                                      
                                      slotTimer2()
                                      {
                                         if(result == false )
                                         {
                                      
                                         }
                                      }
                                      

                                      ?

                                      J 1 Reply Last reply 17 Jul 2020, 08:01
                                      0
                                      • T TomNow99
                                        17 Jul 2020, 07:53

                                        @jsulm Hmmm, but how? Something like:

                                        in QTimerSlot ( check evert 3 sec ) I have:

                                        connectionSocket->connectToHost("192.168.43.23", 139);
                                        timer2.start(1000); // maybe singleShot
                                        
                                        

                                        and how to connect information from slotConnected and other QTimer?

                                        slotConnected()
                                        {
                                            result = true;
                                        }
                                        
                                        slotTimer2()
                                        {
                                           if(result == false )
                                           {
                                        
                                           }
                                        }
                                        

                                        ?

                                        J Offline
                                        J Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on 17 Jul 2020, 08:01 last edited by
                                        #19

                                        @TomNow99 Yes, like this

                                        1 Reply Last reply
                                        1
                                        • T Offline
                                          T Offline
                                          TomNow99
                                          wrote on 17 Jul 2020, 10:43 last edited by
                                          #20

                                          @jsulm Thank you

                                          1 Reply Last reply
                                          0

                                          6/20

                                          16 Jul 2020, 18:46

                                          topic:navigator.unread, 14
                                          • Login

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