## 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.
silver.moon
Experienced Solver
Posts: 67
Joined: Fri Feb 20, 2015 6:13 am

### Best way to monitor internet connectivity in a wx app

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

whoops
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

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 ]

doublemax
Moderator
Posts: 15154
Joined: Fri Apr 21, 2006 8:03 pm

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

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!

whoops
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

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 ]

doublemax
Moderator
Posts: 15154
Joined: Fri Apr 21, 2006 8:03 pm
Location: \$FCE2

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

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!

whoops
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

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 ]