Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TuToRiAl] How to Check if theres any Internet Connection [TuToRiAl]
#1
Hey guys. I have not posted a tutorial in weeks. So I thought this thread would be very helpful; A lot of VB applications I make need an active internet connection in order to work, so we will use a function to detect whether theres an active internet connection at the moment or not. This can help prevent fatal errors while the application requires
internet when trying to run something when there is no internet connection.


Heres the code, first plug in the Function anywhere between 2 classes as in "Form1_Load and CheckBox1_CheckState."

[Image: 074efb6a7a57a844513dc3ae25cfc8da.png]

Function We Are Using:

Code:
Public Function IsConnectionAvailibe() As Boolean
        Dim objURL As New System.Uri("http://192.168.1.1/")

        Dim ObjWebRequest As System.Net.WebRequest
        ObjWebRequest = System.Net.WebRequest.Create(objURL)
        Dim objResp As System.Net.WebResponse

        Try
            objResp = ObjWebRequest.GetResponse
            objResp.Close()
            ObjWebRequest = Nothing
            Return True

        Catch ex As Exception
            objResp = Nothing
            ObjWebRequest = Nothing
            Return False

        End Try
    End Function

Now to manipulate the code a little bit:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If IsConnectionAvailibe() = True Then 'If there was internet on at the moment the application opened, add statements to enable the application for internet usage.
            Label1.Visible = False
            TextBox2.Enabled = True
            TextBox1.Enabled = True
            Button1.Enabled = True
            Button2.Enabled = True
            Button3.Enabled = True
        ElseIf IsConnectionAvailibe() = False Then 'If there wasn't internet when the application opened, add statements to disable internet using items to prevent errors.
            Label1.Visible = True
            TextBox2.Enabled = False
            TextBox1.Enabled = False
            Button1.Enabled = False
            Button2.Enabled = False
            Button3.Enabled = False
        End If
    End Sub
End Class

Now obviously you may change it from Form1_Load to a BackroundWorker or Timer. Thats just me. Enjoy! Post replies if you have questions!
[Image: t5BWm.png]
Reply
#2
Great code.

Personally, I'd just ping a network using My.Computer.Network.Ping, like google for example, and check the return value. A lot simpler and less code. Regardless, yours is great.
Reply
#3
Really good code but it could be shorter.
[Image: breaky.png]
Reply
#4
Thanks for the tutorial Smile
And great code!
Reply
#5
Hell of a lot of code for such a simple task.

Reply
#6
(01-08-2011, 05:57 PM)Fragma Wrote: Hell of a lot of code for such a simple task.

Excuse me for living! lol

This is one of the most efficient ways to verify a solid internet connection.
[Image: t5BWm.png]
Reply
#7
thanks for the tutorial.
great job.
Blackhat
~ven0m
[Image: 3dtextven0mhf.png]
Reply
#8
You should Never use try/catch to work out if something like the internet connection is available, always handle the return value!
Reply
#9
(01-09-2011, 06:48 PM)Modest Wrote: You should Never use try/catch to work out if something like the internet connection is available, always handle the return value!

...No thanks, i'd stick to my piece of code thank you. And there are NO returns to be handled that are displayed, in my awesome code. We use the try, catch ex as exception, and end try. It is my way to go, it is the safest way to go. You only have 5 posts non the less, a true Learner. See ya later alligator.
[Image: t5BWm.png]
Reply
#10
This is alot of code for a simple task ^^ but good job on this bro.
Feel free to Send Me A PM if you need help.
I can give you my contact info from there.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [HELP]Tutorial/Learning System[HELP] norBATman 2 1,327 09-08-2012, 10:17 PM
Last Post: AceInfinity
  [TuToRiAl] How to make Offline (No Internet) Captcha (Human Confirmation) [TuToRiAl] Resistance 24 12,675 08-30-2012, 10:14 PM
Last Post: Resistance
  [Visual Basic] FTP Uploader [Tutorial] Coding Support 6 3,042 08-12-2012, 12:36 AM
Last Post: Kenneth
  [RELEASE]SF-TutPad "Tutorial Editor" [RELEASE] Digital-Punk 28 10,311 08-11-2012, 11:25 PM
Last Post: Kenneth
  Emailer Tutorial dunlop03 12 2,901 06-24-2012, 04:33 AM
Last Post: kpn37z

Forum Jump:


Users browsing this thread: 9 Guest(s)