# Checking internet connection

**URL:** <https://forum.xojo.com/t/checking-internet-connection/11165>\
**Category:** General\
**Created:** [September 24, 2013, 4:17am UTC](https://forum.xojo.com/t/checking-internet-connection/11165 "2013-09-24T04:17:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean\_Clancy](https://forum.xojo.com/user_avatar/forum.xojo.com/sean_clancy/32/775_2.png) [@Sean\_Clancy](https://forum.xojo.com/u/Sean_Clancy)\
**Post date:** [September 24, 2013, 4:17am UTC](https://forum.xojo.com/t/checking-internet-connection/11165/1 "2013-09-24T04:17:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thom\_McGrath](https://forum.xojo.com/user_avatar/forum.xojo.com/thom_mcgrath/32/192_2.png) [@Thom\_McGrath](https://forum.xojo.com/u/Thom_McGrath)\
**Post date:** [September 24, 2013, 4:48am UTC](https://forum.xojo.com/t/checking-internet-connection/11165/2 "2013-09-24T04:48:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sean\_Clancy](https://forum.xojo.com/user_avatar/forum.xojo.com/sean_clancy/32/775_2.png) [@Sean\_Clancy](https://forum.xojo.com/u/Sean_Clancy)\
**Post date:** [September 24, 2013, 5:15am UTC](https://forum.xojo.com/t/checking-internet-connection/11165/3 "2013-09-24T05:15:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tomas\_J](https://forum.xojo.com/user_avatar/forum.xojo.com/tomas_j/32/311_2.png) [@Tomas\_J](https://forum.xojo.com/u/Tomas_J)\
**Post date:** [September 24, 2013, 6:10am UTC](https://forum.xojo.com/t/checking-internet-connection/11165/4 "2013-09-24T06:10:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tomas\_J](https://forum.xojo.com/user_avatar/forum.xojo.com/tomas_j/32/311_2.png) [@Tomas\_J](https://forum.xojo.com/u/Tomas_J)\
**Post date:** [September 24, 2013, 6:13am UTC](https://forum.xojo.com/t/checking-internet-connection/11165/5 "2013-09-24T06:13:42Z")

</div>

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:

```auto
ping localhost > filename.tmp
```

---

<div class="post-metadata">

**Author:** ![Sean\_Clancy](https://forum.xojo.com/user_avatar/forum.xojo.com/sean_clancy/32/775_2.png) [@Sean\_Clancy](https://forum.xojo.com/u/Sean_Clancy)\
**Post date:** [September 24, 2013, 7:04am UTC](https://forum.xojo.com/t/checking-internet-connection/11165/6 "2013-09-24T07:04:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tomas\_J](https://forum.xojo.com/user_avatar/forum.xojo.com/tomas_j/32/311_2.png) [@Tomas\_J](https://forum.xojo.com/u/Tomas_J)\
**Post date:** [September 24, 2013, 8:15am UTC](https://forum.xojo.com/t/checking-internet-connection/11165/7 "2013-09-24T08:15:55Z")

</div>

Check this out, on Windows you better use “ping [google.com](http://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.

```auto
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
```

---

<div class="post-metadata">

**Author:** ![John\_Hansen](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@John\_Hansen](https://forum.xojo.com/u/John_Hansen)\
**Post date:** [September 24, 2013, 8:33am UTC](https://forum.xojo.com/t/checking-internet-connection/11165/8 "2013-09-24T08:33:16Z")

</div>

[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:

```auto
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](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.

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [September 24, 2013, 11:50am UTC](https://forum.xojo.com/t/checking-internet-connection/11165/9 "2013-09-24T11:50:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tomas\_J](https://forum.xojo.com/user_avatar/forum.xojo.com/tomas_j/32/311_2.png) [@Tomas\_J](https://forum.xojo.com/u/Tomas_J)\
**Post date:** [September 24, 2013, 1:30pm UTC](https://forum.xojo.com/t/checking-internet-connection/11165/10 "2013-09-24T13:30:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [September 25, 2013, 12:29pm UTC](https://forum.xojo.com/t/checking-internet-connection/11165/11 "2013-09-25T12:29:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sean\_Clancy](https://forum.xojo.com/user_avatar/forum.xojo.com/sean_clancy/32/775_2.png) [@Sean\_Clancy](https://forum.xojo.com/u/Sean_Clancy)\
**Post date:** [September 26, 2013, 6:12am UTC](https://forum.xojo.com/t/checking-internet-connection/11165/12 "2013-09-26T06:12:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Wayne\_Golding](https://forum.xojo.com/user_avatar/forum.xojo.com/wayne_golding/32/165_2.png) [@Wayne\_Golding](https://forum.xojo.com/u/Wayne_Golding)\
**Post date:** [September 26, 2013, 8:31am UTC](https://forum.xojo.com/t/checking-internet-connection/11165/13 "2013-09-26T08:31:23Z")

</div>

On Windows I use this:

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

sh.Execute(“ping [ns1.google.com](http://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](http://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](http://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.

---

<div class="post-metadata">

**Author:** ![Sean\_Clancy](https://forum.xojo.com/user_avatar/forum.xojo.com/sean_clancy/32/775_2.png) [@Sean\_Clancy](https://forum.xojo.com/u/Sean_Clancy)\
**Post date:** [September 26, 2013, 8:51am UTC](https://forum.xojo.com/t/checking-internet-connection/11165/14 "2013-09-26T08:51:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wayne\_Golding](https://forum.xojo.com/user_avatar/forum.xojo.com/wayne_golding/32/165_2.png) [@Wayne\_Golding](https://forum.xojo.com/u/Wayne_Golding)\
**Post date:** [September 26, 2013, 9:08am UTC](https://forum.xojo.com/t/checking-internet-connection/11165/15 "2013-09-26T09:08:45Z")

</div>

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](http://ns1.google.com) -c 1”)  
If InStr(0, sh.Result, “[ns1.google.com](http://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](http://ns1.google.com) -n 1”)  
If InStr(0, sh.Result, “Pinging”) \> 0 Then Return True  
#Else  
sh.Execute(“ping [ns1.google.com](http://ns1.google.com) -c 1”)  
If Instr(0, sh.Result, “[ns1.google.com](http://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

---

<div class="post-metadata">

**Author:** ![Richard\_Summers](https://forum.xojo.com/user_avatar/forum.xojo.com/richard_summers/32/477_2.png) [@Richard\_Summers](https://forum.xojo.com/u/Richard_Summers)\
**Post date:** [June 10, 2015, 10:21am UTC](https://forum.xojo.com/t/checking-internet-connection/11165/16 "2015-06-10T10:21:46Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [June 10, 2015, 10:40am UTC](https://forum.xojo.com/t/checking-internet-connection/11165/17 "2015-06-10T10:40:28Z")

</div>

> [@193206:@Richard Summers](#):
>
> 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?

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

---

<div class="post-metadata">

**Author:** ![Richard\_Summers](https://forum.xojo.com/user_avatar/forum.xojo.com/richard_summers/32/477_2.png) [@Richard\_Summers](https://forum.xojo.com/u/Richard_Summers)\
**Post date:** [June 10, 2015, 10:49am UTC](https://forum.xojo.com/t/checking-internet-connection/11165/18 "2015-06-10T10:49:53Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [June 10, 2015, 11:52am UTC](https://forum.xojo.com/t/checking-internet-connection/11165/19 "2015-06-10T11:52:46Z")

</div>

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
`

---

<div class="post-metadata">

**Author:** ![Richard\_Summers](https://forum.xojo.com/user_avatar/forum.xojo.com/richard_summers/32/477_2.png) [@Richard\_Summers](https://forum.xojo.com/u/Richard_Summers)\
**Post date:** [June 10, 2015, 11:55am UTC](https://forum.xojo.com/t/checking-internet-connection/11165/20 "2015-06-10T11:55:32Z")

</div>

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

[Next page](https://forum.xojo.com/t/checking-internet-connection/11165.md?page=2)
