Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4151

Beginners • Re: Running a program on start up and after connecting to the internet

$
0
0
Have your code ping Google. As soon as it can ping, you have internet, as soon as the ping fails, you have no Internet. It can do stuff while the ping works and stops when the ping fails and wait until the ping succeeds again.

That'll tell you if Google can be reached. It'll tell you nothing about the specific server the code needs. Assuming that because you can reach a Google server you can reach the server you need to is not a sensible thing to do.

Handling the failure when connecting to the desired server is what needs to be done. The only exception to that is if the code/application/person debugging the connection* needs to know which of the (at least) five states things are actually in: no network, no internet, I can reach google but not the target server, I can reach the target server but not google, and I can reach google and the target server.
There are a million different ways to do this.
Yep.

I suspect the OP has fallen into the same trap that a lot of inexperienced programmers do: assuming that because the code works under idea conditions it will always work. Which means they have no error/exception handling and are expecting the OS to do that for them. Which it won't.

That attitude is fine for a quick, single function limited use script. It will quickly bite you in the a??e outside of that.

*: Even then the person doing the debugging can find this out with a few commands. And if they can't they probably won't be able to debug it anyway.

Statistics: Posted by thagrol — Mon Sep 30, 2024 12:47 am



Viewing all articles
Browse latest Browse all 4151

Trending Articles