1

Referring to sample https://wiki.wxwidgets.org/WxHTTP#Basic_Usage

#include <wx/sstream.h>
#include <wx/protocol/http.h>

wxHTTP get;
get.SetHeader(_T("Content-type"), _T("text/html; charset=utf-8"));
get.SetTimeout(10); // 10 seconds of timeout instead of 10 minutes ...

// this will wait until the user connects to the internet. It is important in case of dialup (or ADSL) connections
while (!get.Connect(_T("www.google.com")))  // only the server, no pages here yet ...
    wxSleep(5);

wxApp::IsMainLoopRunning(); // should return true

// use _T("/") for index.html, index.php, default.asp, etc.
wxInputStream *httpStream = get.GetInputStream(_T("/intl/en/about.html"));

// wxLogVerbose( wxString(_T(" GetInputStream: ")) << get.GetResponse() << _T("-") << ((resStream)? _T("OK ") : _T("FAILURE ")) << get.GetError() );

if (get.GetError() == wxPROTO_NOERR)
{
    wxString res;
    wxStringOutputStream out_stream(&res);
    httpStream->Read(out_stream);

    wxMessageBox(res);
    // wxLogVerbose( wxString(_T(" returned document length: ")) << res.Length() );
}
else
{
    wxMessageBox(_T("Unable to connect!"));
}

wxDELETE(httpStream);
get.Close();

So now the while loop in above code waits till internet connection is available.

Wanted to display a message to user to check connection and try again (Yes/No/Retry), solution should work crossplatform

Have tried wxDialUpManager & wxURL ( reference code link1, link2 ) but sample code doesn't work. It doesn't detect if internet is not available.

What is best way to check for internet connection?

Amod Gokhale
  • 2,346
  • 4
  • 17
  • 32

1 Answers1

0

It sounds better to separate the gui and code that checks connection in a thread .

The threads run standalone and communicates connection state to the main gui thread without perturbing gui event loop.

look at this for reference https://doc.qt.io/qt-5/thread-basics.html

sancelot
  • 1,905
  • 12
  • 31
  • can you share example code? Looking for specific code.. I have tried examples shared above in my question and they don't work – Amod Gokhale Mar 16 '22 at 12:16
  • This is really kind of implementation you will need : https://www.bogotobogo.com/Qt/Qt5_QThreads_GuiThread.php – sancelot Mar 18 '22 at 12:15