What is Best Practice for checking a network connection?

Hello all,

I have a console app that I am trying to debug - it uses URLConnection to send data to Twilio so a text can be sent.

In my logs, I see where the following code come back with a 0.

Dim nic As integer = System.NetworkInterfaceCount

Am I using this correctly or should I use another function? Either way, what would be the best practice when the NetworkInterfaceCount goes to zero?

Thanks,
Tim

You’ll probably want to check out this thread.

The question comes up now and then. On macOS there is NSPathMonitor from the MBS plugin. Do not use 1.1.1.1 or 8.8.8.8 because those can be blocked. I had a user recently who had to use different DNS servers mandated by his ISP.

Thanks for the tip Beatrix
Tim

Thanks Patrick,
I’ll take a look right now!

Actually, I have already read that.

I do not have a bug per-se simply because I handle it. What I am asking is when there is no network connection available, what is the best way to handle that? ie, reboot, retry? Close it or something else?

Or is there a Linux command to restart a connection?

The interesting thing is, there are several apps that are running on this machine. 3 or 4 use the network. The others appear to continue to work - although the others do not have any code to do a check periodically.

BTW, this is on Linux for Raspberry Pi 4.

Tim

I think this is up to you and how you want to craft the user experience.

If you think this will be a temporary kind of issue, you could simply let the user know and give them a button to manually retry (while maybe automatically trying based upon a timer). Rebooting could be a possibility here too but how often do you think that’ll be successful versus the negative user experience of a restart? Of course you’d need to code around a perpetual issue where the device would continuously reboot.

Otherwise if this is a more human-less scenario, you might simply want to poll and check for the network until things come online. Of course you might want some way of notifying someone that things aren’t working. I’m not a big Raspberry Pi person myself, but assuming you can put a red LED, play a sound or some other more physical notification if this is a more commonplace issue for you.

I don’t think Xojo is going to have anything low level enough to restart the network interfaces for you. Instead you could probably just do some shell commands to make this happen depending upon the distro you’re using. But in this scenario you’ll want to be very careful and code more defensively than normal so that you don’t end up in a scenario where you make things worse (e.g. turn off a network interface that then fails to ever get turned back on either through your code or some other edge case).

Not to mention that you’d probably need root permission to do that.

1 Like

The answer to the thread title is that you just attempt your connection and handle the failure. There are so many variables at play that making a test connection in many cases has no value to you.

5 Likes

Thanks guys,

This is 100% automated and yes, it can have root (sudo) privileges.

Thanks,
Tim

.

1 Like