I've asked a question about pinging a URL before, but I wanted something that was a bit shorter and still just as efficient. libcurl seemed to be the perfect answer. Here is my method:
/**
* @fn int testConnection(void)
* @brief Pings "https://www.google.com/" to test if there is an internet connection. Timeout is set to three seconds.
* @return Success value if connected to the internet.
*/
int testConnection(void)
{
CURL *curl;
CURLcode res = 0;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com/");
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return res;
}
Is there anything "wrong" with this pinging method? Are there any ways that I could make this method more efficient?