Best way to detect if you have a valid internet connection?

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Best way to detect if you have a valid internet connection?

896件の閲覧回数
ironsean
Contributor V

Hello,

I'm experiencing an issue with my FRDM-K64F board running MQX 4.2.2. I'm building a firmware that depends on internet connections and works through a wireless adapter over the Ethernet port(tiny routers in client mode essentially). But these adapters seem to be inconsistent, losing connection, etc. This in turn leads to my firmware appearing to lock up. I installed a bunch of watchdog timers, but they don't seem to be doing the trick.

I'll need to do some more in depth testing to see what happens when there's no internet connection and why things lock, but in the meantime:

Is there a good way to check if I have a valid connection to the outside world? I'm probably going to put a check in a task, with that if failed will set the device reset bit and let me start again.

Sean

ラベル(1)
タグ(2)
0 件の賞賛
2 返答(返信)

663件の閲覧回数
soledad
NXP Employee
NXP Employee

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

663件の閲覧回数
ironsean
Contributor V

Thanks, I'll give this a try!

0 件の賞賛