Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    How to check internet connection in Qt

    General and Desktop
    2
    4
    3345
    Loading More Posts
    • 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.
    • S
      saravanavelu39 last edited by

      Hi all,,
      @bool networkInterface::networkAvailable()
      {
      QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
      bool result = false;

      for (int i = 0; i < ifaces.count(); i++)
      {
          QNetworkInterface iface = ifaces.at(i);
          if ( iface.flags().testFlag(QNetworkInterface::IsUp)
               && !iface.flags().testFlag(QNetworkInterface::IsLoopBack) )
          {
      
              for (int j=0; j<iface.addressEntries().count(); j++)
              {
                  if (result == false)
                      result = true;
              }
          }
      
      }
      return result;
      

      }
      @

      this code is checking internet and working correctly in windows,,,but if i disconnect the internet in ubuntu means still the online status is true,,Please give me a suggesstion,,
      Thanks,,

      1 Reply Last reply Reply Quote 0
      • J
        jafarabadi.qt last edited by

        Hi saravanavelu39, How are you friend? :)
        I have a simple solution :)) very simple!!!
        just ping 8.8.8.8 ip and check output...
        Look at this:
        @QProcess pingProcess;
        QString exec = "ping";
        QStringList params;
        params << "-c" << "1" << IP;
        pingProcess.start(exec,params,QIODevice::ReadOnly);
        pingProcess.waitForFinished(-1);
        QString p_stdout = pingProcess.readAllStandardOutput();
        QString p_stderr = pingProcess.readAllStandardError();@

        1 Reply Last reply Reply Quote 0
        • S
          saravanavelu39 last edited by

          Hi Jafar,,
          Thanks for your reply,,,I used your code,,that is working correctly in ubuntu,,but the application some time hangs after using this code,,so could you please give me a some other suggesstion,,

          Thanks,

          1 Reply Last reply Reply Quote 0
          • J
            jafarabadi.qt last edited by

            of course saravanavelu..
            There are some solution in this link
            i hope be useful :)
            http://ubuntuforums.org/showthread.php?t=1803980

            1 Reply Last reply Reply Quote 0
            • First post
              Last post