Checking internet connection

What’s the best way to check if the computer is connected to the net using the least amount of code?
I’m using an online database in my desktop app and I need it to tell the user to connect to the internet.
Cheers,
Sean

Honestly, just try to connect, there’s no better way. You can try connecting to Google, but a positive result doesn’t mean much. Your server could be down, the route to your server could be down, or the user’s firewall could block the port / request. So the only accurate test is to try.

is there a shell command I can use to ping a site?

As Thom mentioned, the fact that you may or may not ping a server doesn’t mean that the service/port (http) is reachable.

For instance I am using linux proxy server squid with squidguard filters for all my customers, blocking away Ads and Banners e.g. On desktop side they cannot ping anything in the internet nor use any messengers or Apps using direct internet connections as long as they do not use a proxy server.

The best way would be to use try…catch exception handling for your code. To check httpstatus if you are using HTMLViewer Control and so on.

and to answer your question regarding shell commands. You can direct any shell output into a file and read this file back.
In windows you may use this for instance:

ping localhost > filename.tmp

Hi Tomas - is this a shell command - is there a short coding line that can do this>
Sean

Check this out, on Windows you better use “ping google.com”. Hope it helps, but don’t forget. A pingable host doesn’t mean that its services are available. Otherwise a host in stealth mode doesn’t react on pings but still offers its services on other ports.

try

Dim sh As New Shell
sh.Execute("ping -c 5 google.com")
If sh.ErrorCode = 0 Then MsgBox str(sh.Result) Else MsgBox "Error: " + Str(sh.ErrorCode)

catch

MsgBox "Unknown Error"

Finally

end try

[quote=36056:@Tomas Jakobs]and to answer your question regarding shell commands. You can direct any shell output into a file and read this file back.
In windows you may use this for instance:

ping localhost > filename.tmp

Ping is not always the best way to test with, as some companies or ISP are blocking the ping request from their firewalls.

Most companies allows (Outbound) port 80 (http), and 443 (https), So why not just use the TCPSocket (http://documentation.xojo.com/index.php/TCPSocket)

Connect to a known web server on port 80, and if you have connection, the connected event is raised otherwise the error event.

Ping and Traceroute are both ICMP based and most engineers will block inbound icmp packets into their Firewall’s DMZ(s) hosting their Virtual or Physical servers and/or Ingress BGP Border Routers.

Well Mike John you both are right… as Thom and I said before :slight_smile:
But I did not want to stop this conversation leaving Sean behind or unsatisfied on this.

Tomas absolutely and I appreciated your code posting. Anytime I see network related threads I get excited since I can actually provide value there :wink: Xojo programming I am still a newbie :slight_smile:

I really appreciate your comments.
I have some webservices setup on website do do with buying licenses and activation confirmation services.
These are all to be accessed inside my desktop application…
Of course, I want my users to be connected to the internet for activation, license buying and first time run.
However, the problem was getting an msgbox telling them that they need to connect to the net if they want to do any of these 3 things.

On Windows I use this:

[code]
Sub AmIConnected() As Boolean
Dim sh As New Shell
sh.Mode = 0

sh.Execute(“ping ns1.google.com -n 1”)
If InStr(0, sh.Result, “Pinging”) > 0 Then Return True
Return False
End Sub[/code]

Although it looks like a ping it actually uses the result of the ns look up to find the address of the server. The result of this ping will start with:

Pinging ns1.google.com [216.239.32.10]

A fail will start with in:

Ping request could not find host…

Ideally you’d replace ns1.google.com with your own server, however this is a single packet ping (-n 1).

Be aware though that running this repeatedly may be construed as a DOS attack & break the client’s internet connection.

Does it work on Mac also? I’m aiming my software at Windows and Mac (it’s music software).
I’ll probably be running it at the fastest 2 minutes apart.
For example, one person would buy a license (check online) and then activate the computer (also checks online).
…and when they run it first time, it also checks for onliness (?)
Cheers,
Sean

On Linux & I’m assuming OSX you would use:

[code]Sub AmIConnected() As Boolean
Dim sh As New Shell
sh.Mode = 0

sh.Execute(“ping ns1.google.com -c 1”)
If InStr(0, sh.Result, “ns1.google.com”) > 0 Then Return True
Return False
End Sub[/code]

So

[code]Sub AmIConnected() As Boolean
Dim sh As New Shell
sh.Mode = 0

#If TargetWin32 Then
sh.Execute(“ping ns1.google.com -n 1”)
If InStr(0, sh.Result, “Pinging”) > 0 Then Return True
#Else
sh.Execute(“ping ns1.google.com -c 1”)
If Instr(0, sh.Result, “ns1.google.com”) > 0 Then Return True
#EndIf
Return False
End Sub[/code]

Should work.

Your usage won’t be a problem. In Windows a standard ping is four packets one after the other (or four attempts with failures). On Linux (and I’m assuming OSX) it’s continuous until you break

Hi,
I am trying to use the code below to check for an internet connection, but I keep receiving the 2 Msgboxs stating TCP Socket Error and No Internet Connection Established?

[code]Dim TCPSocket1 As New TCPSocket

TCPSocket1.Address = “http://www.google.com
TCPSocket1.Port = 80

TCPSocket1.Connect

While Not TCPSocket1.IsConnected

If TCPSocket1.LastErrorCode <> 0 then
  MsgBox("TCP Socket Error!")
  Exit
End If

TCPSocket1.Poll

Wend

If TCPSocket1.IsConnected then
MsgBox(“connected to the Internet”)
Else
MsgBox(“No Internet Connection Established!”)
End If

TCPSocket1.Close[/code]

Any ideas what I am doing wrong?

Thank you all in advance.

Why don’t you use the MUCH simpler shell to Ping, as Tomas Jacobs or Wayne Golding have posted ?

Michel,
the answer to your question is because the above posts state that:

  1. Ping’s are not always reliable, due to firewalls etc.

  2. Could break the user’s internet connection due to repetitivness.

Therefore, I would prefer to test for a connection via a different method :slight_smile:

Instead of 18 lines of non working TCPSocket code, why not instead simply use HTTPSocket and 4 lines only that work ?

Function IsConnected() As Boolean Dim http As New HTTPSocket http.yield = True dim ih as InternetHeaders = (http.GetHeaders("http://www.google.com", 1)) return (ih <> nil) End Function

Because I never had 4 lines of working code - but now I do :slight_smile:
Thanks Michel.