Check if computer is CONNECTED to internet?
-
Saturday, December 29, 2012 5:08 PM
Hello, I know there has been other questions like this, but none of the answers worked for me.
I would like to check whether the computer is connected to the internet or not. I've got this code, but the program crashes everytime that I disconnect from the internet. The code is in a timer that runs every second btw.
If My.Computer.Network.Ping("www.google.com") Then
Connection.Image = My.Resources.high_connection
Else
Connection.Image = My.Resources.no_connection
End IfThanks, Andreas
All Replies
-
Saturday, December 29, 2012 5:37 PM
I know you don't and that's a problem with Windows.
With asychronous returns you do not have to poll. It is like a event.
Renee
"MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
-
Saturday, December 29, 2012 5:38 PMModerator
Hi Andreas,
Before you execute the ping, first ensure that there is an active network connection on the PC. I'd also suggest pinging a root DNS server since google could decide to one day block ICMP traffic, or could even be down (as unlikely as that is). The root DNS server will be cloned several times so the IP address should always respond. I'd wrap the whole thing up in a little function and then call that in your code:
Public Function IsInternetAvailable() As Boolean 'first make sure that a network is available on the PC If Not My.Computer.Network.IsAvailable Then Return False 'now try to ping a root dns server Return My.Computer.Network.Ping("198.41.0.4") End FunctionAnd then use it like:
If IsInternetAvailable() Then Connection.Image = My.Resources.high_connection Else Connection.Image = My.Resources.no_connection End If
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
- Proposed As Answer by Cor LigthertMVP Saturday, December 29, 2012 6:47 PM
-
Saturday, December 29, 2012 5:43 PMThanks Reed, but it still crashes when I disconnect from the internet, I dont know if I'am doing anything wrong. It is this line btw. Return My.Computer.Network.Ping("198.41.0.4") Thanks, Andreas
-
Saturday, December 29, 2012 5:47 PMModerator
You can also respond to the local connection state change without polling by handling an event:
Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load AddHandler My.Computer.Network.NetworkAvailabilityChanged, AddressOf ConnectionStateChange End Sub Private Sub ConnectionStateChange(sender As Object, e As EventArgs) If IsInternetAvailable() Then Connection.Image = My.Resources.high_connection Else Connection.Image = My.Resources.no_connection End If End Sub Public Function IsInternetAvailable() As Boolean 'first make sure that a network is available on the PC If Not My.Computer.Network.IsAvailable Then Return False 'now try to ping a root dns server Return My.Computer.Network.Ping("198.41.0.4") End Function End Class
Now, this would not cause the image to change if your router lost connection to the internet. You would still have to poll to determine connectivity all the way through to the internet. You could probably set your timer to 5 or 10 seconds though, as every second might be a bit excessive.Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
-
Saturday, December 29, 2012 5:49 PMModerator
I guess you're catching it in the middle of a ping. Increasing the timer interval would probably help, but I suppose you'll need to catch that possible exception. Modify the function to be:
Public Function IsInternetAvailable() As Boolean 'first make sure that a network is available on the PC If Not My.Computer.Network.IsAvailable Then Return False 'now try to ping a root dns server Try Return My.Computer.Network.Ping("198.41.0.4") Catch ex As Exception Return False End Try End FunctionReed Kimble - "When you do things right, people won't be sure you've done anything at all"
-
Saturday, December 29, 2012 5:52 PM
I can't put the
Private Sub ConnectionStateChange(sender As Object, e As EventArgs) If IsInternetAvailable() Then Connection.Image = My.Resources.high_connection Else Connection.Image = My.Resources.no_connection End If End Subin my timer, and if I disconnect from the internet it doesn't change image, but it doesn't crash :D
I use a timer for a clock (with seconds, so it updates every second), should I use 2 timers?
Thanks, Andreas
-
Saturday, December 29, 2012 6:00 PMModerator
No, you could use both and reduce the timer frequency. Use the event to immediately catch the local connection state change and use a longer running timer to check for loss of connection to the internet.
On further thought, you probably should not ping a root DNS server on a regular interval... I'm not sure what the exact rules are but you can't flood a DNS server with pings. Its ok to use them to verify connectivity occasionaly, but not every second. I'm sure google has similar rules as well. No body wants a constant string of pings to their servers. =P
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
-
Saturday, December 29, 2012 6:04 PM
When I was doing it in 2008, they didn't care.
Renee
"MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
-
Saturday, December 29, 2012 6:04 PM
Hehe, but is there another way to check if the computer is connected to the internet? Because I can't get your code to work. :-)
Thanks, Andreas
-
Saturday, December 29, 2012 6:08 PMModerator
What doesn't work?
Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load AddHandler My.Computer.Network.NetworkAvailabilityChanged, AddressOf ConnectionStateChange End Sub Private Sub ConnectionStateChange(sender As Object, e As EventArgs) If IsInternetAvailable() Then Connection.Image = My.Resources.high_connection Else Connection.Image = My.Resources.no_connection End If End Sub Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick If IsInternetAvailable() Then Connection.Image = My.Resources.high_connection Else Connection.Image = My.Resources.no_connection End If End Sub Public Function IsInternetAvailable() As Boolean 'first make sure that a network is available on the PC If Not My.Computer.Network.IsAvailable Then Return False 'now try to ping a root dns server Try Return My.Computer.Network.Ping("198.41.0.4") Catch ex As Exception Return False End Try End Function End ClassReed Kimble - "When you do things right, people won't be sure you've done anything at all"
- Marked As Answer by Mark Liu-lxfModerator Monday, January 07, 2013 2:34 AM
-
Saturday, December 29, 2012 6:16 PM
Here:
Public Function IsInternetAvailable() As Boolean 'first make sure that a network is available on the PC If Not My.Computer.Network.IsAvailable Then Return False 'now try to ping a root dns server Return My.Computer.Network.Ping("198.41.0.4") End Function
At: Return My.Computer.Network.Ping("198.41.0.4") when I disconnect from the internet
Will someone be annoyed if i ping to a "root dns server"? (No body wants a constant string of pings to their servers. =P)
-
Saturday, December 29, 2012 6:20 PM
Hello Andreas98cool,
Hehe, but is there another way to check if the computer is connected to the internet? Because I can't get your code to work. :-)
Thanks, Andreas
try this sample it use api , is c# , but easy convertable in VbNet , http://sdrv.ms/VfJUxP
Regards.
- Carmelo La Monica
- Visual Basic Tips e Tricks Blog
- WordPress.com Blog
- Blogger
- CrystalwebDotNetGroup
- Marked As Answer by Mark Liu-lxfModerator Monday, January 07, 2013 2:34 AM
-
Saturday, December 29, 2012 6:22 PM
How do I convert it into VB? I'am not really an expert :-)Hello Andreas98cool,
Hehe, but is there another way to check if the computer is connected to the internet? Because I can't get your code to work. :-)
Thanks, Andreas
try this sample it use api , is c# , but easy convertable in VbNet , http://sdrv.ms/VfJUxP
Regards.
- Carmelo La Monica
- Visual Basic Tips e Tricks Blog
- WordPress.com Blog
- Blogger
- CrystalwebDotNetGroup
-
Saturday, December 29, 2012 6:23 PMModeratorYou are supposed to replace that with the version that uses a Try-Catch block around the ping.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
-
Saturday, December 29, 2012 6:24 PM
But your still polling.....
Renee
"MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
-
Saturday, December 29, 2012 6:25 PM
Hello Andreas98cool,
How do I convert it into VB? I'am not really an expert :-)
try this converter http://www.developerfusion.com/tools/convert/csharp-to-vb/
Regards.
- Carmelo La Monica
- Visual Basic Tips e Tricks Blog
- WordPress.com Blog
- Blogger
- CrystalwebDotNetGroup
-
Saturday, December 29, 2012 6:43 PM
You could use a try catch statement to keep your application from crashing.
Imports System.Net.NetworkInformation Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Timer1.Interval = 5000 Timer2.Interval = 10 Timer1.Start() End Sub Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick RichTextBox1.Text = "" Timer2.Start() Timer1.Stop() End Sub Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick For Each face As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces() RichTextBox1.AppendText(face.Description & vbCrLf) RichTextBox1.AppendText(face.Name) RichTextBox1.AppendText(face.NetworkInterfaceType.ToString() & vbCrLf) RichTextBox1.AppendText(face.OperationalStatus.ToString() & vbCrLf) RichTextBox1.AppendText(face.Speed & vbCrLf & vbCrLf) Timer1.Start() Timer2.Stop() Next Dim UP As String = "UP" Dim index1 As Integer = 0 While index1 <> -1 index1 = RichTextBox1.Find(UP, index1, RichTextBoxFinds.WholeWord) If index1 <> -1 Then RichTextBox1.SelectionStart = index1 RichTextBox1.SelectionLength = UP.Length RichTextBox1.SelectionColor = Color.Green index1 += UP.Length End If End While Dim DOWN As String = "DOWN" Dim index2 As Integer = 0 While index2 <> -1 index2 = RichTextBox1.Find(DOWN, index2, RichTextBoxFinds.WholeWord) If index2 <> -1 Then RichTextBox1.SelectionStart = index2 RichTextBox1.SelectionLength = DOWN.Length RichTextBox1.SelectionColor = Color.Red index2 += DOWN.Length End If End While RichTextBox1.Select(0, 0) Try If My.Computer.Network.Ping("www.google.com") Then Me.Text = "Internet Good" Else Me.Text = "Internet Down" End If Catch ex As Exception Me.Text = (ex.Message) End Try End Sub End Class
You've taught me everything I know but not everything you know.
- Marked As Answer by Mark Liu-lxfModerator Monday, January 07, 2013 2:34 AM
-
Saturday, December 29, 2012 6:48 PM
You can also do outside VB a Ping by using the Dos box.
I think you should first investigate what Reed wrote that it is not the problem seems strange to me.
Success
Cor -
Saturday, December 29, 2012 7:11 PMI'm not an expert, so what would be the easiest way to do this? :-)
- Edited by Andreas98cool Saturday, December 29, 2012 7:11 PM Forgot smiley
-
Saturday, December 29, 2012 7:56 PM
That's great EXCEPT that you have used polling.
Renee
"MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
-
Saturday, December 29, 2012 9:02 PMSo there is no easy solution/code that I maybe can understand? :-)
-
Sunday, December 30, 2012 8:36 AM
So there is no easy solution/code that I maybe can understand? :-)
I'm afraid not.
But don't care these forums are not made for one person who ask a question, but to solve as a knowledge base.
Most persons will understand it after they have tried the given solutions and if not tell what is the result of their attempts, the latter you seems to avoid in all ways.
Success
Cor- Edited by Cor LigthertMVP Sunday, December 30, 2012 6:15 PM
-
Sunday, December 30, 2012 2:36 PM
Try this ... should works
This works without pinging and annoying any server
(Add a COM reference to "Network List Type Library" (NETWORKLIST.DLL) )
Private Function IsConnectedToInternet() As Boolean If System.Environment.OSVersion.Version.Major >= 6 Then 'Vista and Up Dim Networks As New NETWORKLIST.NetworkListManager If Networks.IsConnectedToInternet Then Return True Else Return False End If Else 'XP and less Dim flags As UInteger = 0 If InternetGetConnectedState(flags, 0) Then Return True Else Return False End If End If End Function Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByVal Flag As UInteger, ByVal Reserved As UInteger) As Boolean
If you like, you can also do it like this .. It will save you from having a reference to the COM library
Private Function IsConnectedToInternet() As Boolean If System.Environment.OSVersion.Version.Major >= 6 Then 'Vista and Up 'Dim Networks As New NETWORKLIST.NetworkListManager Dim Networks = Activator.CreateInstance(Type.GetTypeFromCLSID(New Guid("{DCB00C01-570F-4A9B-8D69-199FDBA5723B}"))) If Networks.IsConnectedToInternet Then Return True Else Return False End If Else 'XP and less Dim flags As UInteger = 0 If InternetGetConnectedState(flags, 0) Then Return True Else Return False End If End If End Function Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByVal Flag As UInteger, ByVal Reserved As UInteger) As Boolean
- Edited by CrazypennieMicrosoft Community Contributor Sunday, December 30, 2012 2:59 PM
- Proposed As Answer by Renee Culver Sunday, December 30, 2012 3:48 PM
- Marked As Answer by Mark Liu-lxfModerator Monday, January 07, 2013 2:34 AM
-
Sunday, December 30, 2012 3:47 PM
"This works without pinging and annoying any server"
Yay Crazypennie! You eliminated polling!!!
Renee
"MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
-
Sunday, December 30, 2012 5:47 PMModerator
"This works without pinging and annoying any server"
Yay Crazypennie! You eliminated polling!!!
Renee
"MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
You can't "eliminate polling"... the objective is to test for internet connectivity... this requires some kind of polling routine. In this case the user still has to call IsConnectedToInternet() on some kind of interval.
Crazypennie appears to have found the underlying mechanizm used by Windows to test for internet connectivity, and thats great.... no need to reinvent the wheel. However, there is still likely an ICMP packet or short TCP connection to some server in order to verify connectivity (perform a packet capture while this method is executing).
While the documentation does not explicity define how the method works, it may be that it is just performing a root dns query (actually querying DNS versus pinging the server), similar to what I suggested initially.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
-
Sunday, December 30, 2012 9:38 PM
That should be changed to "You cant eliminate polling on windows." Asynchronous techniques are something most of windows programming is missing although one may ask how a passive presence is 'checked". Perhaps the question itself is not meaningful.
Renee
"MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
-
Monday, December 31, 2012 1:47 AM
An interresting fact is that the class DCB00C01-570F-4A9B-8D69-199FDBA5723B has an event
NetworkConnectivityChanged
This event will fire whenever the IPV4 or IPV6 internet connectivity changes.
The bad part, is that all that is exposed from the class is an interface ... And I cannot find out how to implement the event sink
-
Monday, December 31, 2012 5:34 AM
Hooray!!!!!!
Polling stinks as a technique.
Renee
"MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
-
Monday, December 31, 2012 8:19 AM
Try this ... should works
This works without pinging and annoying any server
(Add a COM reference to "Network List Type Library" (NETWORKLIST.DLL) )
Private Function IsConnectedToInternet() As Boolean If System.Environment.OSVersion.Version.Major >= 6 Then 'Vista and Up Dim Networks As New NETWORKLIST.NetworkListManager If Networks.IsConnectedToInternet Then Return True Else Return False End If Else 'XP and less Dim flags As UInteger = 0 If InternetGetConnectedState(flags, 0) Then Return True Else Return False End If End If End Function Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByVal Flag As UInteger, ByVal Reserved As UInteger) As Boolean
If you like, you can also do it like this .. It will save you from having a reference to the COM library
Private Function IsConnectedToInternet() As Boolean If System.Environment.OSVersion.Version.Major >= 6 Then 'Vista and Up 'Dim Networks As New NETWORKLIST.NetworkListManager Dim Networks = Activator.CreateInstance(Type.GetTypeFromCLSID(New Guid("{DCB00C01-570F-4A9B-8D69-199FDBA5723B}"))) If Networks.IsConnectedToInternet Then Return True Else Return False End If Else 'XP and less Dim flags As UInteger = 0 If InternetGetConnectedState(flags, 0) Then Return True Else Return False End If End If End Function Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByVal Flag As UInteger, ByVal Reserved As UInteger) As Boolean
It appears to work without polling anything. I used Microsoft Network Monitor 3.4 with it and WireShark current rev free Network Monitor with the following code and I didn't see any polling occuring (note that I couldn't find NETWORK.Dll to reference so I had to reference add reference, COM, Network List Manager 1.0 Type Library which only allow 1/2 of your code to work. InternetGetConnectedState was unreferenced). However Microsoft Network Monitor 3.4 with the code running kept knocking out my internet connection while wireshark caused no problems. Never had a problem with Microsoft Network Monitor 3.4 before.
Anyhow the code I used follows along with two images, top image is MS Net Mon which brought down my network. Bottom image is Wireshark which no problems with network occured. No polling is seen at all in the Network Monitors. Also note that in the top image the connection between my P/C and wireless modem is up because I'm connected to the wireless modem but the wireless connection status shows the internet being down (bottom right corner of the image).
Public Class Form1 'Add reference, COM, Network List Manager 1.0 Type Library Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Timer1.Interval = 10000 Timer1.Start() End Sub Private Function IsConnectedToInternet() As Boolean If System.Environment.OSVersion.Version.Major >= 6 Then 'Vista and Up Dim Networks As New NETWORKLIST.NetworkListManager If Networks.IsConnectedToInternet Then Return True Else Return False End If Else MessageBox.Show("Your OS is incompatable") End If End Function Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick Me.Text = CStr(IsConnectedToInternet()) End Sub End Class
You've taught me everything I know but not everything you know.
- Edited by Mr. MonkeyboyMicrosoft Community Contributor Monday, December 31, 2012 9:18 AM
-
Monday, December 31, 2012 11:19 AM
Monkeyboy,
Network List Manager 1.0 Type Library --- NETWORKLIST.DLL --- the class DCB00C01-570F-4A9B-8D69-199FDBA5723B
All those are the same thing, just different way to access it.
-------------------------------------------------------
The second part of the code is using wininet.dll.
those libraries mainly targetting XP and before ... However they works on Vista.
On Window7 some get you to a dead lock
-------------------------------------------------------
- Edited by CrazypennieMicrosoft Community Contributor Monday, December 31, 2012 11:25 AM
-
Monday, December 31, 2012 3:45 PMI'm not really sure why you need to know this. It's possible to have an Internet connection, but there is no way to know whether a particular host is available until a specific request is initiated (which is how browser software handles this issue). In other words, you have to code for a request failure (Try...Catch) regardless of whether a connection exists.
Paul ~~~~ Microsoft MVP (Visual Basic)
-
Monday, December 31, 2012 5:28 PM
Paul it just seems to the unaquainted that they should have it.
Renee
"MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
-
Monday, December 31, 2012 6:20 PM
MonkeyBoy,
Where did the network Monitor come from?
Renee
"MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
-
Tuesday, January 01, 2013 12:09 AM
I'm not really sure why you need to know this. It's possible to have an Internet connection, but there is no way to know whether a particular host is available until a specific request is initiated (which is how browser software handles this issue). In other words, you have to code for a request failure (Try...Catch) regardless of whether a connection exists.
Paul ~~~~ Microsoft MVP (Visual Basic)
I do agree whit what you are saying here, All this is interresting as general knowledge, but it would be rarely usefull.
However, I can see some use for that, ... An application may choose not to propose to the user to check for upgrade if internet is not there .. or thing like that.
It is sure not usefull to the user since he already have an icon in the task bar telling him if internet is available.
... Another usage of this would be to recreate this icon in the case of an application that use a custom desktop without explorer ... like a kiosk app.
-
Tuesday, January 01, 2013 12:11 AM
Renee
Wireshark is free from http://www.wireshark.org/
Microsoft Network Monitor is free from http://www.microsoft.com/en-us/download/details.aspx?id=4865
I recommend Wiresharks, it's much better.
You've taught me everything I know but not everything you know.
- Edited by Mr. MonkeyboyMicrosoft Community Contributor Tuesday, January 01, 2013 12:14 AM
-
Tuesday, January 01, 2013 12:25 AM
Thank you So much, Monkeyboy!!! The network monitor Fatals out upon installation But Wireshark does fine!!!
Renee
"MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me
-
Tuesday, January 01, 2013 1:18 AMYou're welcome! :)
You've taught me everything I know but not everything you know.

