Best way to monitor internet connectivity in a wx app

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
User avatar
silver.moon
Experienced Solver
Experienced Solver
Posts: 67
Joined: Fri Feb 20, 2015 6:13 am

Best way to monitor internet connectivity in a wx app

Post by silver.moon » Sat Aug 15, 2015 4:50 am

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

User avatar
whoops
Earned a small fee
Earned a small fee
Posts: 23
Joined: Sat Jun 27, 2015 5:53 am
Location: China

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

Post by whoops » Sat Aug 15, 2015 6:46 am

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]
Last edited by whoops on Sat Aug 15, 2015 7:22 am, edited 1 time in total.
 Things being equal, the simplest explanation tends to be the right.

 [ Windows 7 Ultimate x64 | wxWidgets 3.0.2 | Microsoft Visual C++ 2010 Express ]

User avatar
doublemax
Moderator
Moderator
Posts: 15260
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

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

Post by doublemax » Sat Aug 15, 2015 7:19 am

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.
Use the source, Luke!

User avatar
whoops
Earned a small fee
Earned a small fee
Posts: 23
Joined: Sat Jun 27, 2015 5:53 am
Location: China

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

Post by whoops » Sat Aug 15, 2015 7:57 am

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]
 Things being equal, the simplest explanation tends to be the right.

 [ Windows 7 Ultimate x64 | wxWidgets 3.0.2 | Microsoft Visual C++ 2010 Express ]

User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2293
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

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

Post by evstevemd » Sat Aug 15, 2015 5:36 pm

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).
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
[Ubuntu 19.04/Windows 10 Pro/MacOS 10.13 - GCC/MinGW/Clang, CodeLite IDE]

User avatar
whoops
Earned a small fee
Earned a small fee
Posts: 23
Joined: Sat Jun 27, 2015 5:53 am
Location: China

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

Post by whoops » Sun Aug 16, 2015 12:34 am

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]
Last edited by whoops on Sun Aug 16, 2015 12:27 pm, edited 1 time in total.
 Things being equal, the simplest explanation tends to be the right.

 [ Windows 7 Ultimate x64 | wxWidgets 3.0.2 | Microsoft Visual C++ 2010 Express ]

User avatar
doublemax
Moderator
Moderator
Posts: 15260
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

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

Post by doublemax » Sun Aug 16, 2015 10:10 am

Did you check that it actually returns "offline" / receive an event when you're connected through a router and unplug the network cable?
Use the source, Luke!

User avatar
whoops
Earned a small fee
Earned a small fee
Posts: 23
Joined: Sat Jun 27, 2015 5:53 am
Location: China

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

Post by whoops » Sun Aug 16, 2015 11:52 am


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]
 Things being equal, the simplest explanation tends to be the right.

 [ Windows 7 Ultimate x64 | wxWidgets 3.0.2 | Microsoft Visual C++ 2010 Express ]

User avatar
doublemax
Moderator
Moderator
Posts: 15260
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

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

Post by doublemax » Sun Aug 16, 2015 12:13 pm

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.
Use the source, Luke!

User avatar
whoops
Earned a small fee
Earned a small fee
Posts: 23
Joined: Sat Jun 27, 2015 5:53 am
Location: China

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

Post by whoops » Sun Aug 16, 2015 12:29 pm

it's very kind of you, doublemax, i've updated my previous answer. :-)
 Things being equal, the simplest explanation tends to be the right.

 [ Windows 7 Ultimate x64 | wxWidgets 3.0.2 | Microsoft Visual C++ 2010 Express ]

Post Reply