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. Checking if a device has an active internet connection
QtWS25 Last Chance

Checking if a device has an active internet connection

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 7 Posters 3.0k 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.
  • E Offline
    E Offline
    EricoDeMecha
    wrote on last edited by EricoDeMecha
    #1

    Re: How to check whether the computer connects the internet?

    Problem:
    Check if a device has an active internet connection. The solutions I have found so far are either not cross-platform or blocking. However, while reading the network documentation in Qt, I cam across QNetworkInformation which seems to support and do this efficiently.

    Code Sample:

    #include <QCoreApplication>
    #include <QNetworkInformation>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QNetworkInformation* net_info = QNetworkInformation::instance();
        
        if(net_info->reachability() == QNetworkInformation::Reachability::Online)
        {
            qDebug() << "Device is Online";
        }else
        {
            qDebug() << "Device is offline";
        }
        return a.exec();
    }
    
    

    The above program crashes. I have tried debugging it but could not arrive at a reasonable conclusion.

    Question: If you have used this library before, would you share on a section that checks for online connectivity?

    JoeCFDJ Cobra91151C 2 Replies Last reply
    0
    • W Offline
      W Offline
      wrosecrans
      wrote on last edited by
      #2

      As I said in the previous thread you linked,

      What's the underlying question? Trying to determine if you are connected to the Internet is almost never useful information, and it's almost impossible to define well.

      Is there a particular service you want to connect to? Just try to connect to that. Don't check if there's an internet connection an then only try to connect to the service if you think there's an Internet connection. You will drive users crazy when your Internet check fails despite having a route to your service. And when things work, it will be slower for no reason while you spend time trying to convince yourself there is an Internet connection.

      1 Reply Last reply
      1
      • E EricoDeMecha

        Re: How to check whether the computer connects the internet?

        Problem:
        Check if a device has an active internet connection. The solutions I have found so far are either not cross-platform or blocking. However, while reading the network documentation in Qt, I cam across QNetworkInformation which seems to support and do this efficiently.

        Code Sample:

        #include <QCoreApplication>
        #include <QNetworkInformation>
        
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
        
            QNetworkInformation* net_info = QNetworkInformation::instance();
            
            if(net_info->reachability() == QNetworkInformation::Reachability::Online)
            {
                qDebug() << "Device is Online";
            }else
            {
                qDebug() << "Device is offline";
            }
            return a.exec();
        }
        
        

        The above program crashes. I have tried debugging it but could not arrive at a reasonable conclusion.

        Question: If you have used this library before, would you share on a section that checks for online connectivity?

        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #3

        @EricoDeMecha net_info is a nullptr.

        JoeCFDJ 1 Reply Last reply
        0
        • JoeCFDJ JoeCFD

          @EricoDeMecha net_info is a nullptr.

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #4

          @JoeCFD

              #include <QCoreApplication>
              #include <QNetworkInformation>
          
              int main(int argc, char *argv[])
              {
                  QCoreApplication a(argc, argv);
          
                  if ( QNetworkInformation::loadDefaultBackend() && QNetworkInformation::loadBackendByFeatures( QNetworkInformation::Features::Reachability ) ) {
                      QNetworkInformation* net_info = QNetworkInformation::instance();
                      if ( nullptr != net_info ) {
                          if(net_info->reachability() == QNetworkInformation::Reachability::Online) {
                              qDebug() << "Device is Online";
                          } 
                          else {
                              qDebug() << "Device is offline";
                          }  
                      }
                 }
          
                  return a.exec();
              }
          
          
          
          R 1 Reply Last reply
          0
          • E EricoDeMecha

            Re: How to check whether the computer connects the internet?

            Problem:
            Check if a device has an active internet connection. The solutions I have found so far are either not cross-platform or blocking. However, while reading the network documentation in Qt, I cam across QNetworkInformation which seems to support and do this efficiently.

            Code Sample:

            #include <QCoreApplication>
            #include <QNetworkInformation>
            
            int main(int argc, char *argv[])
            {
                QCoreApplication a(argc, argv);
            
                QNetworkInformation* net_info = QNetworkInformation::instance();
                
                if(net_info->reachability() == QNetworkInformation::Reachability::Online)
                {
                    qDebug() << "Device is Online";
                }else
                {
                    qDebug() << "Device is offline";
                }
                return a.exec();
            }
            
            

            The above program crashes. I have tried debugging it but could not arrive at a reasonable conclusion.

            Question: If you have used this library before, would you share on a section that checks for online connectivity?

            Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by
            #5

            @EricoDeMecha

            Hello!

            Additionally, you can use the QTcpSocket (https://doc.qt.io/qt-6/qtcpsocket.html) and connect to your website or google.com to check for the Internet connection. Please, check out my code below:

            bool Dialog::checkNetConnection()
            {
                bool isConnected = false;
                QTcpSocket сonnectionSocket;
                сonnectionSocket.connectToHost("google.com", 80);
                сonnectionSocket.waitForConnected(4000);
            
                if (сonnectionSocket.state() == QTcpSocket::ConnectedState) {
                    isConnected = true;
                }
            
                сonnectionSocket.close();
                return isConnected;
            }
            
            qDebug() << "Is online: " << checkNetConnection();
            

            Happy coding!

            JonBJ 1 Reply Last reply
            0
            • Cobra91151C Cobra91151

              @EricoDeMecha

              Hello!

              Additionally, you can use the QTcpSocket (https://doc.qt.io/qt-6/qtcpsocket.html) and connect to your website or google.com to check for the Internet connection. Please, check out my code below:

              bool Dialog::checkNetConnection()
              {
                  bool isConnected = false;
                  QTcpSocket сonnectionSocket;
                  сonnectionSocket.connectToHost("google.com", 80);
                  сonnectionSocket.waitForConnected(4000);
              
                  if (сonnectionSocket.state() == QTcpSocket::ConnectedState) {
                      isConnected = true;
                  }
              
                  сonnectionSocket.close();
                  return isConnected;
              }
              
              qDebug() << "Is online: " << checkNetConnection();
              

              Happy coding!

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Cobra91151 Absolutely! But what happens when google.com goes out of business? ;-)

              Cobra91151C 1 Reply Last reply
              0
              • JonBJ JonB

                @Cobra91151 Absolutely! But what happens when google.com goes out of business? ;-)

                Cobra91151C Offline
                Cobra91151C Offline
                Cobra91151
                wrote on last edited by
                #7

                @JonB

                In such case he can use his own website or other websites, for example: "1.1.1.1" (cloudflare) instead of "google.com". :)

                JonBJ 1 Reply Last reply
                0
                • Cobra91151C Cobra91151

                  @JonB

                  In such case he can use his own website or other websites, for example: "1.1.1.1" (cloudflare) instead of "google.com". :)

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @Cobra91151
                  CloudFlare is now a Google Cloud Platform Technology Partner So they will be brought down with Google :D

                  Cobra91151C 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @Cobra91151
                    CloudFlare is now a Google Cloud Platform Technology Partner So they will be brought down with Google :D

                    Cobra91151C Offline
                    Cobra91151C Offline
                    Cobra91151
                    wrote on last edited by
                    #9

                    @JonB

                    Ok, good point. I would suggest to use one of the Public DNS Server from this list: https://public-dns.info/ ;-)

                    1 Reply Last reply
                    1
                    • JoeCFDJ JoeCFD

                      @JoeCFD

                          #include <QCoreApplication>
                          #include <QNetworkInformation>
                      
                          int main(int argc, char *argv[])
                          {
                              QCoreApplication a(argc, argv);
                      
                              if ( QNetworkInformation::loadDefaultBackend() && QNetworkInformation::loadBackendByFeatures( QNetworkInformation::Features::Reachability ) ) {
                                  QNetworkInformation* net_info = QNetworkInformation::instance();
                                  if ( nullptr != net_info ) {
                                      if(net_info->reachability() == QNetworkInformation::Reachability::Online) {
                                          qDebug() << "Device is Online";
                                      } 
                                      else {
                                          qDebug() << "Device is offline";
                                      }  
                                  }
                             }
                      
                              return a.exec();
                          }
                      
                      
                      
                      R Offline
                      R Offline
                      RaptaG
                      wrote on last edited by RaptaG
                      #10

                      @JoeCFD besides return a.exec(), what is the point of QCoreApplication?

                      JonBJ 1 Reply Last reply
                      0
                      • R RaptaG

                        @JoeCFD besides return a.exec(), what is the point of QCoreApplication?

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

                        @RaptaG
                        It provides all the (non-UI) services Qt requires/offers (signals, slots, lots & lots of other stuff, initializations)... basically a Qt program won;t work without one, else it's a not a program actually using Qt.

                        R 1 Reply Last reply
                        1
                        • JonBJ JonB

                          @RaptaG
                          It provides all the (non-UI) services Qt requires/offers (signals, slots, lots & lots of other stuff, initializations)... basically a Qt program won;t work without one, else it's a not a program actually using Qt.

                          R Offline
                          R Offline
                          RaptaG
                          wrote on last edited by
                          #12

                          @JonB Can I borrow your code and use it on my open-source project (GPL3)? I will credit you.

                          JonBJ 1 Reply Last reply
                          0
                          • R RaptaG

                            @JonB Can I borrow your code and use it on my open-source project (GPL3)? I will credit you.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #13

                            @RaptaG
                            I don't know what code you are thinking of and not sure what you could want? But you are welcome to whatever, no credit necessary.

                            R 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @RaptaG
                              I don't know what code you are thinking of and not sure what you could want? But you are welcome to whatever, no credit necessary.

                              R Offline
                              R Offline
                              RaptaG
                              wrote on last edited by
                              #14

                              @JonB

                              Something else now, if I use it in a bool function, then return a.exec() is not necessary, right?

                              Also, how likely is it for the first 2 if statements not to be the case?

                              jsulmJ 1 Reply Last reply
                              0
                              • R RaptaG

                                @JonB

                                Something else now, if I use it in a bool function, then return a.exec() is not necessary, right?

                                Also, how likely is it for the first 2 if statements not to be the case?

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @RaptaG said in Checking if a device has an active internet connection:

                                return a.exec() is not necessary, right?

                                Wrong.
                                If you're writing a Qt application which requires the event loop to be running (most Qt applications) then you have to call exec()...

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

                                R 1 Reply Last reply
                                0
                                • jsulmJ jsulm

                                  @RaptaG said in Checking if a device has an active internet connection:

                                  return a.exec() is not necessary, right?

                                  Wrong.
                                  If you're writing a Qt application which requires the event loop to be running (most Qt applications) then you have to call exec()...

                                  R Offline
                                  R Offline
                                  RaptaG
                                  wrote on last edited by
                                  #16

                                  @jsulm so, how do i adjust it so that it works in a bool function?

                                  JonBJ 1 Reply Last reply
                                  0
                                  • R RaptaG

                                    @jsulm so, how do i adjust it so that it works in a bool function?

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by JonB
                                    #17

                                    @RaptaG
                                    I really don't think we know what you are asking about here.

                                    There is nothing to stop you calling exec() in a function, boolean or not.

                                    bool foo()
                                    {
                                        int unusedReturnResult = someApplicationObject.exec();
                                        // ignore that return result
                                        return false;    // whatever boolean value you want
                                    }
                                    

                                    However, since exec() is the entry point to running the main Qt application event loop you are only going to want to call it once (and it does not exit till Qt quits event loop). So I cannot imagine what/why you want to move it to some function which returns a boolean. Makes me thing you are going to use it oddly. If you are thinking of calling this each time you want to check "if a device has an active internet connection" this is not the architecture.

                                    R 1 Reply Last reply
                                    1
                                    • JonBJ JonB

                                      @RaptaG
                                      I really don't think we know what you are asking about here.

                                      There is nothing to stop you calling exec() in a function, boolean or not.

                                      bool foo()
                                      {
                                          int unusedReturnResult = someApplicationObject.exec();
                                          // ignore that return result
                                          return false;    // whatever boolean value you want
                                      }
                                      

                                      However, since exec() is the entry point to running the main Qt application event loop you are only going to want to call it once (and it does not exit till Qt quits event loop). So I cannot imagine what/why you want to move it to some function which returns a boolean. Makes me thing you are going to use it oddly. If you are thinking of calling this each time you want to check "if a device has an active internet connection" this is not the architecture.

                                      R Offline
                                      R Offline
                                      RaptaG
                                      wrote on last edited by
                                      #18

                                      @JonB said in Checking if a device has an active internet connection:

                                      If you are thinking of calling this each time you want to check "if a device has an active internet connection"

                                      That's what I was planning to do tbh 😅

                                      jsulmJ 1 Reply Last reply
                                      0
                                      • R RaptaG

                                        @JonB said in Checking if a device has an active internet connection:

                                        If you are thinking of calling this each time you want to check "if a device has an active internet connection"

                                        That's what I was planning to do tbh 😅

                                        jsulmJ Offline
                                        jsulmJ Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        @RaptaG said in Checking if a device has an active internet connection:

                                        That's what I was planning to do tbh

                                        Why? You will block your application. Qt is an asynchronous framework - you should use it as such. QTcpSocket is also asynchronous...

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

                                        R 1 Reply Last reply
                                        0
                                        • jsulmJ jsulm

                                          @RaptaG said in Checking if a device has an active internet connection:

                                          That's what I was planning to do tbh

                                          Why? You will block your application. Qt is an asynchronous framework - you should use it as such. QTcpSocket is also asynchronous...

                                          R Offline
                                          R Offline
                                          RaptaG
                                          wrote on last edited by
                                          #20

                                          @jsulm I am a newbie and async stuff look really difficult. Even then, I only need to check for internet connection very few times/day in my program

                                          jsulmJ 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