Page 1 of 1

Best way to monitor internet connectivity in a wx app

Posted: Sat Aug 15, 2015 4:50 am
by silver.moon
Hi

My app does things mostly with the internet, so I need a mechanism to get notified when the internet went off.
And again when it came back up.

So that certain tasks can be put on hold till the connectivity is available

Re: Best way to monitor internet connectivity in a wx app

Posted: Sat Aug 15, 2015 6:46 am
by whoops
internet went off

which you mean that the internet is cut off?
you can simply test the following code:

Code: Select all

    #include <wx/protocol/http.h>
    
    wxHTTP http;
    wxString host("www.bing.com");
    if ( http.Connect(host) )
        wxLogMessage( wxT("Connected") );
    else
        wxLogMessage( wxT("Disconnected") );
but notice that, change wxString host to random host, e.g random.host.i.dont.know.nett
wxString host("random.host.i.dont.know.nett"); is also works! but function works fine.
so you can make it to a fast website, for example, google, bing, yahoo...
[/size]

Re: Best way to monitor internet connectivity in a wx app

Posted: Sat Aug 15, 2015 7:19 am
by doublemax
My app does things mostly with the internet, so I need a mechanism to get notified when the internet went off. And again when it came back up.
All you can do is to try to read an external url periodically and check if it works.

Re: Best way to monitor internet connectivity in a wx app

Posted: Sat Aug 15, 2015 7:57 am
by whoops
here is another way to test internet connection, in a recently question of mine answered by doublemax:

Code: Select all

    #include <wx/url.h>
    wxURL url(wxT("http://www.codecogs.com/latex/js/eq_config.js"));
    if( url.IsOk() )
        wxMessageBox( url.GetInputStream() ? "Connected" : "Disconnected" );
    else
        wxMessageBox( "Unknown error" );
note that http://www.codecogs.com/latex/js/eq_config.js is something like CDN(Content Delivery Network), we can use this to test internet connection :-)
there are some CDN address: or goto http://www.staticfile.org/ to choose by yourself.
[/size]

Re: Best way to monitor internet connectivity in a wx app

Posted: Sat Aug 15, 2015 5:36 pm
by evstevemd
Have you tried wxDialupManager? It seems that it does that. I have never tried it though
Docs says
This class encapsulates functions dealing with verifying the connection status of the workstation (connected to the Internet via a direct connection, connected through a modem or not connected at all) and to establish this connection if possible/required (i.e.

in the case of the modem).

Re: Best way to monitor internet connectivity in a wx app

Posted: Sun Aug 16, 2015 12:34 am
by whoops
the following code may also better than previous answers:

Code: Select all

    //#include <wx/dialup.h>
    wxDialUpManager *dialUp = wxDialUpManager::Create();

    if ( dialUp && dialUp->IsOk() )
        wxLogMessage( dialUp->IsOnline() ? wxT("Online") : wxT("Offline") );
    else
        wxLogError( wxT("Unknown error to create wxDialUpManager") );

    delete dialUp;  // REMEMBER TO DELETE IT
i've tried under windows, it works, and here is the documentation on trunk: http://docs.wxwidgets.org/trunk/classwx ... nager.html
and here have two useful events you may use:
EVT_DIALUP_CONNECTED(func):
A connection with the network was established.
EVT_DIALUP_DISCONNECTED(func):
The connection with the network was lost.
i hope it helpful to you. :-)
[/size]

Re: Best way to monitor internet connectivity in a wx app

Posted: Sun Aug 16, 2015 10:10 am
by doublemax
Did you check that it actually returns "offline" / receive an event when you're connected through a router and unplug the network cable?

Re: Best way to monitor internet connectivity in a wx app

Posted: Sun Aug 16, 2015 11:52 am
by whoops

in that case, e.g. when
receive an event when you're connected through a router and unplug the network cable

we can use two useful events to listen the plugin event and unplug event blow:
•EVT_DIALUP_CONNECTED(func):
A connection with the network was established.

•EVT_DIALUP_DISCONNECTED(func):
The connection with the network was lost.

it can give a instant(real) status of internet connection.
but i don't know if the OP or doublemax care about the distinction between a real offline(connected, but internet not available) and doesn't plug in the cable.
but in wxDialUpManager, i do not find a function that can check the detailed status of internet connection.

but here i have a question to ask doublemax:

Code: Select all

delete dialUp;  // if it's necessary to delete wxDialUpManager pointer manually?
or when the function is over, the wxWidgets will auto delete the wxDialUpManager object?
[/size]

Re: Best way to monitor internet connectivity in a wx app

Posted: Sun Aug 16, 2015 12:13 pm
by doublemax

Code: Select all

delete dialUp;  // if it's necessary to delete wxDialUpManager pointer manually?
Yes. Only objects that have some kind of "parent" will be deleted automatically by their parent. E.g. windows or sizers that are assigned to a window.

And there are some methods that take a pointer where the documentation explicitly states that the object takes ownership of the pointer in which case you don't have to (and are not allowed to) delete it yourself.

Re: Best way to monitor internet connectivity in a wx app

Posted: Sun Aug 16, 2015 12:29 pm
by whoops
it's very kind of you, doublemax, i've updated my previous answer. :-)