Checking internet connection

:slight_smile:

If using Windows, there is a call in the Win Functionality Suite to “InternetGetConnectedState” which returns:

I haven’t tried it myself.

BTW, the language reference suggests this:
https://documentation.xojo.com/index.php/System.Network

Dim ipAddress As String ipAddress = System.Network.LookupIPAddress("RealSoftware.com") If ipAddress <> "" Then MsgBox(ipAddress) Else MsgBox("An error occurred.") End If

But this code may block your UI for some seconds, if there is no connection.

I did not know about that DNS search. Interesting.

On Mac and iOS, there is SCNetworkReachability

Jason King created a class using the framework to check connectivity. see https://forum.xojo.com/18059-online-or-not

I am sure it must be easy to do the same.

That said, since a Desktop computer seldom uses Cell connectivity, it maybe not be necessary.

The sample of @Michel Bujardet using HTTPSocket is the preferable solution to this question I think. HTTP-socket-object works on all platforms and using SHELL always feels somewhat tricky to me so I try to avoid it.
But: Think it should run in another tread since it holds your code-execution for at least the time-out period when not able to connect to the host.

[quote=193414:@Joost Rongen]The sample of @Michel Bujardet using HTTPSocket is the preferable solution to this question I think. HTTP-socket-object works on all platforms and using SHELL always feels somewhat tricky to me so I try to avoid it.
But: Think it should run in another tread since it holds your code-execution for at least the time-out period when not able to connect to the host.[/quote]

If you look at my snippet, it has http.yield = True which precisely avoids execution hold. A thread would be overkill.

May let the system check this for you?

check wiki: System.Network.IsConnected or System.Network.LookupIPAddress(“Destination”)

ex from wiki:

Dim IPAddress As String
IPAddress = System.Network.LookupIPAddress(“wikipedia.org”)
If IPAddress <> “” Then
MsgBox(IPAddress)
Else
MsgBox(“An error occurred”)
End If

[quote=193439:@Christian Bader]May let the system check this for you?

check wiki: System.Network.IsConnected or System.Network.LookupIPAddress(“Destination”)

ex from wiki:

Dim IPAddress As String
IPAddress = System.Network.LookupIPAddress(“wikipedia.org”)
If IPAddress <> “” Then
MsgBox(IPAddress)
Else
MsgBox(“An error occurred”)
End If
[/quote]

Oliver Osswald already posted it 6 hours ago. He noted that may block the UI until the DNS responds.

I tested under Win7 before I posted my reply here, and found out that it takes a few seconds to get back if there is no internet connection at all.
The original goal was just a simple method returning True or False which you can run every now and then, or … ?

[quote=193451:@Joost Rongen]I tested under Win7 before I posted my reply here, and found out that it takes a few seconds to get back if there is no internet connection at all.
The original goal was just a simple method returning True or False which you can run every now and then, or … ?[/quote]

The WFS method suggested by Peter Job that taps into the Windows framework responds instantly under Windows 10. It should be just as fast in 7. Look for the Query method in the InternetConnectionWFS module. It returns an integer with 0 or n, but is easy to modify to return a boolean.

WFS is at https://github.com/arbp/WFS

I am building an application to be used by a salesman in a minibus. He’s normally connected thru 4G. For this case it would be handy if I could give him a red bullet if not connected and let application keep data in a local database. I don’t want to have my application frozen at any time, especially not when it’s waiting for no internet connection. That’s why I suggested to have a tread running and a timer checking the status periodically. And … I try to avoid too much dependency on the MS Windows platform. So Michel, think using HTTP socket and a thread would still be an acceptable idea … or …

The isConnected HTTPSocket.GetHeaders method works fine even without a thread. With HTTP.Yield = True it never blocks the UI, so placing it into a timer is fine without the need for a thread. It responds immediately when the connection is here, but it needs to get to the end of the timeout of one second to report an absence of connection. I shall think this is acceptable, especially since 4G connections usually are slower to report disconnection.

I played with a rectangle and this in a 100 ms timer

if IsConnected then Rectangle1.FillColor = &c00FF0000 else Rectangle1.FillColor = &cFF000000 end if

I’ve found that if your computer has a network connection but the network isn’t connected to the internet, that this method will block the UTI even with .Yield = True and/or run in a thread. I’ve seen it take several minutes to return the result that the internet isn’t available. I’ve switched to using a shell ping command in a thread and so far that seems to be working OK even with a balky network.

I just checked again on Windows leaving the LAN on and just stopping the Internet connection. Still faithfully reports not connected after the set 1 second timeout.

For Joost anyway, he is probably not on a LAN as he mentions a 4G connection.

I like a shell to ping for its simplicity, however. Frankly, I do not understand the fear and prejudice often manifested against shell commands. After all, it is an API native to the OS too.

As I am in the process of completing an application for Windows tablets (When RubberViews does not take all my time :wink: ), I think we will have often to verify connectivity in the future. Desktop computers are usually always connected, but it is far to be the case for tablets.

This is just why I have mostly a local SQLite database beside the internet connection to the server-production database (PostgreSQL). Most of the time the user won’t even experience there is a flaw in the connection because there is quite some cashing.

I see. You download when you have a connection so in fact the work is local, and just update when you have an opportunity ?

Indeed ! Actually updating is synchronizing. (2 way)

Neat :slight_smile: Such an approach could be very useful for some iOS apps.

Don’t underestimate the work on both sides, server and clients. But once you have it, it’s a good basis for more.

Yes, unfortunately yes… and yes… even with .Yield = True and running in a thread.

Having a timer firing Thread1 every second for this test.
Within Tread1 I do:

Dim http As New HTTPSocket mPollingReady = false http.yield = True dim ih as InternetHeaders = (http.GetHeaders(mPollUrl, 1)) InternetConnected = (ih <> nil) mPollingReady = True

When my LAN doesn’t provide internet it takes approximately 10 seconds before the code in the main-tread continues. So I have taken my measures using .Yield and put it in a thread, but no satisfaction. Same for OSX and Windows.