Results 1 to 9 of 9

Thread: Is Internet Connection Available

  1. #1

    Thread Starter
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Question Is Internet Connection Available

    Hi

    I am using VB.Net 2002. I have the following code that works but takes too much time to execute. Any another way (that works ).

    VB Code:
    1. Public Function IsConnectionAvailable() As Boolean
    2.     ' Returns True if connection is available
    3.     ' Replace [url]www.yoursite.com[/url] with a site that
    4.     ' is guaranteed to be online - perhaps your
    5.     ' corporate site, or microsoft.com
    6.     Dim objUrl As New System.Uri("http://www.yoursite.com/")
    7.     ' Setup WebRequest
    8.     Dim objWebReq As System.Net.WebRequest
    9.     objWebReq = System.Net.WebRequest.Create(objUrl)
    10.     Dim objResp As System.Net.WebResponse
    11.     Try
    12.         ' Attempt to get response and return True
    13.         objResp = objWebReq.GetResponse
    14.         objResp.Close()
    15.         objWebReq = Nothing
    16.         Return True
    17.     Catch ex As Exception
    18.         ' Error, exit and return False
    19.         objResp.Close()
    20.         objWebReq = Nothing
    21.         Return False
    22.     End Try
    23. End Function
    'Here’s how you might use this function in your application:
    VB Code:
    1. If IsConnectionAvailable() = True Then
    2.     MessageBox.Show("You are online!")
    3. End If

  2. #2

  3. #3
    Hyperactive Member anita2002's Avatar
    Join Date
    Dec 2001
    Location
    In u'r Heart
    Posts
    482
    What about this

    Public Function chkNetworkStatus() As Boolean

    Dim wr As HttpWebRequest
    Dim ws As HttpWebResponse
    Dim sURL As String
    sURL = "http://some URL here"
    wr = CType(WebRequest.Create(sURL), HttpWebRequest)
    Try
    ws = CType(wr.GetResponse, HttpWebResponse)
    Return True
    Catch exc As Exception
    MsgBox("Off-line - Show appropriate message here ", MsgBoxStyle.Information)
    Return False
    End Try

    End Function
    Last edited by anita2002; Dec 3rd, 2003 at 03:39 AM.
    Can't imagine life without VB
    (Various Boyfriends)

  4. #4

  5. #5

  6. #6
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you can check to see if you are connected to the net very easily if thats what you need , here's a quick example i knocked up for you...
    VB Code:
    1. [Color=Blue]Private[/color] [Color=Blue]Declare[/color] [Color=Blue]Function[/color] InternetGetConnectedState [Color=Blue]Lib[/color] "wininet.dll" ([Color=Blue]ByRef[/color] lpdwFlags [Color=Blue]As[/color] [Color=Blue]Integer[/color], [Color=Blue]ByVal[/color] dwReserved [Color=Blue]As[/color] [Color=Blue]Integer[/color]) [Color=Blue]As[/color] [Color=Blue]Integer
    2.  
    3. [/color]    [Color=Blue]Private[/color] [Color=Blue]Sub[/color] Button1_Click([Color=Blue]ByVal[/color] sender [Color=Blue]As[/color] System.Object, [Color=Blue]ByVal[/color] e [Color=Blue]As[/color] System.EventArgs) [Color=Blue]Handles[/color] Button1.Click
    4.         [Color=Blue]If[/color] InternetGetConnectedState(0, 0) = 1 [Color=Blue]Then
    5. [/color]            MessageBox.Show("you have a Connection to the internet established!")
    6.         [Color=Blue]Else
    7. [/color]            MessageBox.Show("NO Connection!")
    8.         [Color=Blue]End[/color] [Color=Blue]If
    9. [/color]    [Color=Blue]End[/color] [Color=Blue]Sub[/color]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  7. #7
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

  8. #8

    Thread Starter
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Smile Hello dynamic_sysop

    VB Code:
    1. Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Integer, ByVal dwReserved As Integer) As Integer
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         If InternetGetConnectedState(0, 0) = 1 Then
    5.             MessageBox.Show("you have a Connection to the internet established!")
    6.         Else
    7.             MessageBox.Show("NO Connection!")
    8.         End If
    9.     End Sub
    This code shows message you have a Connection to the internet established! even though i am off-line. What is the problem ? Is it due to my Cable Internet connection.

    Thanks for replying.

  9. #9

    Thread Starter
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Smile Hi maged

    Link provided by you is not working. It displays the a webpage with the message
    Your email message has been idle and this link has become inactive. To access the link, close this window and return to your MSN Hotmail Message. Then click the browser's Refresh button or close your message and reopen it.

    Thanks for replying.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width