Hello Sean,
There is not an algorithm already implemented for that but you can use the function getaddrinfo() to get the same to a host (i.e. freescale.com) if the function returns different than NULL then you have internet connection otherwise you don’t have it. You can put this into an infinite loop that checks every 5 seconds or every minute and you will be able to monitor the internet connectivity.
Below you can find an example using getaddrinfo() function:
struct addrinfo *addrinfo_result;
struct addrinfo *addrinfo_result_first;
int32_t retval;
char addr_str[RTCS_IP6_ADDR_STR_SIZE];
_mem_zero(&addrinfo_hints, sizeof(addrinfo_hints));
addrinfo_hints.ai_flags = AI_CANONNAME;
retval = getaddrinfo("www.example.com", NULL, NULL, &addrinfo_result);
if (retval == 0)
{
addrinfo_result_first = addrinfo_result;
/* Print all resolved IP addresses.*/
while(addrinfo_result)
{
if(inet_ntop(addrinfo_result->ai_family,
&((struct sockaddr_in6 *)((*addrinfo_result).
ai_addr))->sin6_addr,
addr_str, sizeof(addr_str)))
{
printf("\t%s\n", addr_str);
}
addrinfo_result = addrinfo_result->ai_next;
}
freeaddrinfo(addrinfo_result_first);
}
else
{
printf("Unable to resolve host\n");
}
Have a great day,
Sol
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------