Hello There, Guest!
View New Posts  |  View Recent Posts
[VB.Net] Tutorial.[VB.NET] Check if you have an internet connection

  • 0 Vote(s) - 0 Average


07-12-2012, 05:46 AM #1
Gemster
Junior Member
**
Posts: 41 Threads:9 Joined: Jul 2012 Reputation: 4

Tutorial.[VB.NET] Check if you have an internet connection
Hi, I have seen loads of people asking how to check for an internet connection on a lot of forums.

I see many people posting and using httwebrequest to check or by downloading a file but there is 1 simple way that ill show you.

This method may not be the best but it works and it is very simple.

Add this function
Code:
Function IsConnected() As Boolean
Return My.Computer.Network.Ping("209.85.147.147")
End Function

Thats it, Yes really thats the code, it pings google, If it gets a response it is True otherwise False.

You can use it like this:
Code:
If IsConnected() = True Then
MsgBox("Connected")
ElseIf IsConnected() = False Then
MsgBox("Not Connected")
End If

Not much to post for a tutorial but im sick of seeing people asking for this, and sick of people using massive codes to check with httpwebrequest and downloading a temp file.

You can also check if a site is online or not with the same code :D

Note: it maybe best to ping google to get the ip for your local area as google has many ips and you normally connect to the closest one.

Thanks and enjoy :D
This post was last modified: 07-12-2012, 05:49 AM by Gemster.

07-12-2012, 08:44 AM #2
KoBE
¯\_(ツ)_/¯
*******
Administrators
Posts: 4,786 Threads:478 Joined: Jun 2011 Reputation: 65

RE: Tutorial.[VB.NET] Check if you have an internet connection
As you said, you could subsitute the IP for http://google.com It will do the host lookup for you.

Thanks for the tip. I'm sure this will help someone.

07-12-2012, 11:25 AM #3
Gemster
Junior Member
**
Posts: 41 Threads:9 Joined: Jul 2012 Reputation: 4

RE: Tutorial.[VB.NET] Check if you have an internet connection
Well most programs made these days need an internet connection, so why not get them to check for a connection befor using it ? I have seen many programs throw FITS because the user has the internet turned off.

I used google because its always online but you can use microsoft ect.

Thanks

07-12-2012, 07:50 PM #4
AceInfinity
Developer
*******
Administrators
Posts: 9,585 Threads:983 Joined: Jun 2011 Reputation: 75

RE: Tutorial.[VB.NET] Check if you have an internet connection
I would use this:
Code:
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()

As a boolean to check for available internet access.

There are events in System.Net.NetworkInformation.NetworkChange as well that would allow you to monitor network availability.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

07-12-2012, 08:17 PM #5
Shintaro
Member
**
Posts: 247 Threads:58 Joined: Jan 2012 Reputation: 8

RE: Tutorial.[VB.NET] Check if you have an internet connection
(07-12-2012, 07:50 PM)AceInfinity Wrote:  I would use this:
Code:
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()

As a boolean to check for available internet access.

There are events in System.Net.NetworkInformation.NetworkChange as well that would allow you to monitor network availability.
From MSDN:
System.Net.NetworkInformation.NetworkChange
Quote:A network connection is considered to be available if any network
interface is marked "up" and is not a loopback or tunnel interface.
You would still need to send ICMP to see if you had a routable internet connection
This post was last modified: 07-12-2012, 08:17 PM by Shintaro.
Thoughts create realities.


To be old and wise,
You must first be young and stupid.

Try to live an ordinary life,
in a non-ordinary way.

07-12-2012, 08:17 PM #6
KoBE
¯\_(ツ)_/¯
*******
Administrators
Posts: 4,786 Threads:478 Joined: Jun 2011 Reputation: 65

RE: Tutorial.[VB.NET] Check if you have an internet connection
@Ace, simply getting the network status doesn't ensure 'Internet Access' though.. you could be on a network with no access.

07-12-2012, 08:36 PM #7
AceInfinity
Developer
*******
Administrators
Posts: 9,585 Threads:983 Joined: Jun 2011 Reputation: 75

RE: Tutorial.[VB.NET] Check if you have an internet connection
Ahh yeah, not on track today. In that case then i'd recommend this API:

InternetGetConnectedStateEx
More Info - http://msdn.microsoft.com/en-us/library/ms918361.aspx


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

07-12-2012, 08:53 PM #8
AceInfinity
Developer
*******
Administrators
Posts: 9,585 Threads:983 Joined: Jun 2011 Reputation: 75

RE: Tutorial.[VB.NET] Check if you have an internet connection
Actually even InternetGetConnectedState for simple.

Code:
Public Enum INTERNET_Flags
    INTERNET_CONNECTION_CONFIGURED = &H40
    INTERNET_CONNECTION_LAN = &H2
    INTERNET_CONNECTION_MODEM = &H1
    INTERNET_CONNECTION_MODEM_BUSY = &H8
    INTERNET_CONNECTION_OFFLINE = &H20
    INTERNET_CONNECTION_PROXY = &H4
End Enum

Declare Auto Function InternetGetConnectedState Lib "wininet.dll" (lpdwFlags As INTERNET_Flags, dwReserved As Integer) As Boolean

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim InternetConnection As Boolean = InternetGetConnectedState(CType(Marshal.AllocHGlobal(INTERNET_Flags.INTERNET_CONNECTION_CONFIGURED), INTERNET_Flags), 0)

    Console.WriteLine(InternetConnection)
End Sub

EDIT: Or InternetCheckConnection (http://msdn.microsoft.com/en-us/library/windows/desktop/aa384346%28v=vs.85%29.aspx) if you want to check for connectivity to a specific location.
This post was last modified: 07-12-2012, 08:55 PM by AceInfinity.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

07-13-2012, 05:04 AM #9
Gemster
Junior Member
**
Posts: 41 Threads:9 Joined: Jul 2012 Reputation: 4

RE: Tutorial.[VB.NET] Check if you have an internet connection
(07-12-2012, 08:53 PM)AceInfinity Wrote:  Actually even InternetGetConnectedState for simple.

Code:
Public Enum INTERNET_Flags
    INTERNET_CONNECTION_CONFIGURED = &H40
    INTERNET_CONNECTION_LAN = &H2
    INTERNET_CONNECTION_MODEM = &H1
    INTERNET_CONNECTION_MODEM_BUSY = &H8
    INTERNET_CONNECTION_OFFLINE = &H20
    INTERNET_CONNECTION_PROXY = &H4
End Enum

Declare Auto Function InternetGetConnectedState Lib "wininet.dll" (lpdwFlags As INTERNET_Flags, dwReserved As Integer) As Boolean

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim InternetConnection As Boolean = InternetGetConnectedState(CType(Marshal.AllocHGlobal(INTERNET_Flags.INTERNET_CONNECTION_CONFIGURED), INTERNET_Flags), 0)

    Console.WriteLine(InternetConnection)
End Sub

EDIT: Or InternetCheckConnection (http://msdn.microsoft.com/en-us/library/windows/desktop/aa384346(v=vs.85).aspx) if you want to check for connectivity to a specific location.
Yes there are many ways to do these, even a webrequest and getresposne from google.

I did try InternetCheckConnection but its seems unreliable and at times gave false possitives.


Thanks

07-13-2012, 08:17 PM #10
AceInfinity
Developer
*******
Administrators
Posts: 9,585 Threads:983 Joined: Jun 2011 Reputation: 75

RE: Tutorial.[VB.NET] Check if you have an internet connection
(07-13-2012, 05:04 AM)Gemster Wrote:  
(07-12-2012, 08:53 PM)AceInfinity Wrote:  Actually even InternetGetConnectedState for simple.

Code:
Public Enum INTERNET_Flags
    INTERNET_CONNECTION_CONFIGURED = &H40
    INTERNET_CONNECTION_LAN = &H2
    INTERNET_CONNECTION_MODEM = &H1
    INTERNET_CONNECTION_MODEM_BUSY = &H8
    INTERNET_CONNECTION_OFFLINE = &H20
    INTERNET_CONNECTION_PROXY = &H4
End Enum

Declare Auto Function InternetGetConnectedState Lib "wininet.dll" (lpdwFlags As INTERNET_Flags, dwReserved As Integer) As Boolean

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim InternetConnection As Boolean = InternetGetConnectedState(CType(Marshal.AllocHGlobal(INTERNET_Flags.INTERNET_CONNECTION_CONFIGURED), INTERNET_Flags), 0)

    Console.WriteLine(InternetConnection)
End Sub

EDIT: Or InternetCheckConnection (http://msdn.microsoft.com/en-us/library/windows/desktop/aa384346(v=vs.85).aspx) if you want to check for connectivity to a specific location.
Yes there are many ways to do these, even a webrequest and getresposne from google.

I did try InternetCheckConnection but its seems unreliable and at times gave false possitives.


Thanks


It's worked for me, what do you mean though?


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲




Forum Jump:


Possibly Related Threads...
Thread Author Replies Views Last Post
   [Tutorial]Auto Update Tutorial for VB.Net Gemster 8 10,791 02-08-2014, 05:34 AM
Last Post: assasaha
   [Help]SQL Database Telnet connection ! Omegastarscream15 11 2,090 07-19-2013, 12:38 PM
Last Post: Omegastarscream15
   Reading/Writing To INI File For Application Configuration Settings [Tutorial] AceInfinity 16 6,864 07-06-2013, 12:52 AM
Last Post: AceInfinity
   [C#-ASP.NET] SQL Database connection ! Omegastarscream15 3 1,265 06-18-2013, 02:27 PM
Last Post: KoBE
  How to check picture box has been changed t0kneneng 1 933 06-04-2013, 12:00 AM
Last Post: KoBE


Users browsing this thread: 1 Guest(s)