Amod Gokhale
Amod Gokhale

Reputation: 2448

wxwidget - WxHTTP - Check for Internet connection first

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?

Upvotes: 1

Views: 302

Answers (1)

sancelot
sancelot

Reputation: 2063

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

Upvotes: 0

Related Questions