I am developing a C++ network application on Windows. I need to check if internet connection is there or not. I am using gethostbyname()
, but it is giving incorrect information when the system is connected through a proxy. InternetCheckConnection()
and InternetGetConnectedState()
are also not giving reliable results under different conditions. Is there a reliable way to check for internet connectivity covering all the conditions such as proxy and VPN?
Update:
In our company network WinHttpGetProxyForUrl()
is failing with the error ERROR_WINHTTP_AUTODETECTION_FAILED
and WinHttpSendrequest()
is failing with error ERROR_WINHTTP_NAME_NOT_RESOLVED
.
In open network WinHttpSendrequest()
is successful.
To use the ping command, simply open a command prompt and type ping [hostname] or ping [ip address] and then press enter. For example, to check if you can connect to www.google.com, you would type ping www.google.com and press enter.
Click "Open Resource Monitor" at the bottom of the Task Manager window. Go to the "Network" tab. You will see four sections: Processes with Network Activity, Network Activity, TCP Connections, and Listening Ports. In the "Processes with Network Activity" tab, you will see the running processes using network resources.
You can use the function usesInterfaceType(_:) to check which interface type this network path uses. This is the most effective way to figure out if your app is connected over WiFi, cellular or ethernet. print(“It's WiFi!”)
Plain old way !
Include:
#include <wininet.h>
#pragma comment(lib,"Wininet.lib")
In your Method:
char url[128];
strcat(url, "http://www.techtoolbox.com");
bool bConnect = InternetCheckConnection(url, FLAG_ICC_FORCE_CONNECTION, 0);
if (bConnect)
{
//internet connection exists !
}
else
{
//internet DOES NOT connection exists !
}
Hope it helps.
The best way to test the availability of any resource is to try to use it. You only care about the Internet if there is something out there you want to connect to. So, try to connect to it, in the normal course of your program, and handle the errors. Don't try to second-guess what might happen if you try. First, you're trying to predict the future. Second, you aren't necessarily exercising the same things that the actual connection would exercise. Third, your test may succeed and your subsequent use fail due to an intervening condition changing. Fourth, you have to handle the errors from the real use of the resource anyway: why write all that code twice?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With