Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reliable method to check internet connection in C++ application: gethostbyname() failing

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.

like image 286
user1513845 Avatar asked Aug 01 '12 05:08

user1513845


People also ask

How do you check if internet is working using CLI?

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.

How do you check if a program is connected to the internet?

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.

How does Nwpathmonitor check Internet connection?

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!”)


2 Answers

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.

like image 147
bluwater2001 Avatar answered Nov 04 '22 17:11

bluwater2001


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?

like image 4
user207421 Avatar answered Nov 04 '22 17:11

user207421