Check if internet connection is available?

Hi,
does any of you know if is there is a built-in function in Juce to check if an internet connection is available?
Checked Url class already, but maybe there’s another place. Just making sure I didn’t miss something.
Otherwise, a quick check to see if http://www.google.com gives something back could be an alternative I guess :wink:
Or are there other/better ways?
Koen

AFAIK the OSes don’t provide that info - I’d suggest pinging google.com or other big sites as the only reliable way to find out.

Alright, that’s what we’ll do.
Thanks for confirming.
Koen

Hi Jules,

I'd like as well to test my internet connexion;

Could you write down a sample of code that pings a website, please?

Thanks

Just try to read from google.com and see if you get any data back.

There are lots of reasons specific bits of internet might not be available as well.  You have to be careful what you test for.  You could easily be in a situation where google is available but other sites aren't.  If you are behind a company proxy server for example, or other random filtering. 

Ping is very unreliable unless you know what you are doing. 

Also - depending on the situation you may get a quick yes or no, or you may have to wait ages for the connection to time-out. 

What are you trying to achieve? 

Hi, sorry for trouble, I’ve a simple app that want to check if there’s internet connection, if there’s internet connection this code give me a lot of infos in my console, while if there isn’t my app crashes with this error JUCE Message Thread (1): EXC_BAD_ACCESS (code=1, address=0x0) near the line String result = url.createInputStream( false )->readEntireStreamAsString();

Here my code:

      URL url("https://google.com");
         if (url.isWellFormed())
         {
             String result = url.createInputStream(false)->readEntireStreamAsString();
             if (result == "here something that isn't it means that there's no internet connection")
             {
                 DBG("there's no internet");
             }
        }

https://docs.juce.com/master/classURL.html#a51958f3f69f2ecaf3d32a00bc1cd6431

Returns a valid input stream, or nullptr if there was an error trying to open it.

Hence you need to check if url.createInputStream(false) don’t returns nullptr

2 Likes

I wrote this aeons ago that does what you need: squarepine_core/NetworkConnectivityChecker.h at main · SquarePine/squarepine_core · GitHub

3 Likes

Wow, I look at it, amazing!

1 Like