Best way to monitor internet connectivity in a wx app
- 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
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
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") );
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 ]
Re: Best way to monitor internet connectivity in a wx app
All you can do is to try to read an external url periodically and check if it works.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.
Use the source, Luke!
Re: Best way to monitor internet connectivity in a wx app
here is another way to test internet connection, in a recently question of mine answered by doublemax:
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:
[/size]
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" );
there are some CDN address:
- http://cdn.mathjax.org/mathjax/latest/MathJax.js
http://cdn.staticfile.org/jquery/2.1.0/jquery.min.js
http://ajax.aspnetcdn.com/ajax/jquery/j ... 7.2.min.js
http://code.jquery.com/jquery-1.7.2.min.js
http://cdn.staticfile.org/seajs/2.3.0/sea.js
[/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 ]
- evstevemd
- Part Of The Furniture
- Posts: 2408
- Joined: Wed Jan 28, 2009 11:57 am
- Location: United Republic of Tanzania
Re: Best way to monitor internet connectivity in a wx app
Have you tried wxDialupManager? It seems that it does that. I have never tried it though
Docs says
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?
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Re: Best way to monitor internet connectivity in a wx app
the following code may also better than previous answers:
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:
[/size]
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
and here have two useful events you may use:
i hope it helpful to you.EVT_DIALUP_CONNECTED(func):
A connection with the network was established.
EVT_DIALUP_DISCONNECTED(func):
The connection with the network was lost.
[/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 ]
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!
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?
[/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 ]
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?
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!
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 ]