locked
"An exception occurred during a Ping request." - This error message whenever I try to ping a site that is not available.

    Question

  • I am simply trying to ping a website address to see if the website is available or not. Whenever I ping a site that doesn't respond it gives me that error message in the title of this message and throws a program exception. The simple code I am using is below.


    Dim siteAvailable As Boolean

    siteAvailable = My.Computer.Network.Ping("http://www.somesitetotest.com")

    MsgBox(siteAvailable.ToString)


     


    Maybe I am completely overlooking something?? Whenever the site is available and does respond it returns "True" as it should. But whenever it isn't available it throws a exception instead of just simply returning "False" as I thought it should do??

    Thanks for any help.

    PS: Beta "1" did the exact same thing too.


        Jason
    Wednesday, May 11, 2005 6:33 PM

Answers

  • Hi Jason,

    The behavior you're seeing is actually By Design, but it certainly is tricky.  The problem is that the .NET Framework implementation of Ping() differentiates between a host that cannot be resolved (an unknown host) and a host address that can be resolved but can't be ping'ed. For example, if you unplug the machine you're pinging *from* you'll receive an Exception because the network is not available. 

    One way to verify that this is what's causing the exception is to take a look at the exception that is raised:
     - When the Exception is raised in Visual Studio, click the "View Detail..." link on the Exception Assistant.\
     - There is a node called "Inner Exception". Expand it.
     - The "Message" field of the Inner Exception should give you a bit more detail on the cause of the Exception.

    I hope this helps!

    Joe
    The VB Team
    Monday, May 16, 2005 11:20 PM
  • Hi Jason,

    Thanks for enclosing the Stack Trace.

    Unfortunately, there is no way to tell Ping() in My.Computer or System.Net to always return true or false for the reasons stated above.  If you want to write a little wrapper function that will have this behavior, you might try:


    ''' <summary>
    ''' Returns true if the destination is reachable via ping
    ''' </summary>
    Public Function IsDestinationReachable(ByVal hostnameOrAddress As String)
       Dim reachable As Boolean = False
       Try

          reachable = My.Computer.Network.IsAvailable AndAlso _
                           My.Computer.Network.Ping(hostnameOrAddress)

       Catch pingException As System.Net.NetworkInformation.PingException
       Catch genericNetworkException As _ System.Net.NetworkInformation.NetworkInformationException

          ' Fail silently and return false
       
    End Try
       Return reachable
    End Function

     



    You also might want to consider an alternative means of determining if the destination is reachable, as Ping really is not a reliable mechanism for determining if a host is available.  Because ping servers are a security concern, an increasing number of system administrators turn off the ping port on desktops.  Similarly, most firewalls only allow inbound HTTP (Port 80 or 8080) connections.  So you may find that the above function returns true, but the destination is actually reachable in most scenarios.

    If you're currently using Ping() to determine if a resource is available (e.g., a file or web page), then you might consider checking to see if you get an HTTP response from the server, downloading a well-known empty file on the server, etc.  These techniques, though a bit harder to implement, are much more robust.

    I hope this helps!

    Joe
    The VB Team

    Thursday, June 02, 2005 7:52 PM

All replies

  • Hi Jason,

    The behavior you're seeing is actually By Design, but it certainly is tricky.  The problem is that the .NET Framework implementation of Ping() differentiates between a host that cannot be resolved (an unknown host) and a host address that can be resolved but can't be ping'ed. For example, if you unplug the machine you're pinging *from* you'll receive an Exception because the network is not available. 

    One way to verify that this is what's causing the exception is to take a look at the exception that is raised:
     - When the Exception is raised in Visual Studio, click the "View Detail..." link on the Exception Assistant.\
     - There is a node called "Inner Exception". Expand it.
     - The "Message" field of the Inner Exception should give you a bit more detail on the cause of the Exception.

    I hope this helps!

    Joe
    The VB Team
    Monday, May 16, 2005 11:20 PM
  • Hi, thanks alot for the info.

    Here is the exception details that copied to the clipboard.

    ---------------------------------------------------
    System.Net.NetworkInformation.PingException was unhandled
      Message="An exception occurred during a Ping request."
      Source="System"
      StackTrace:
           at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)

           at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer)

           at Microsoft.VisualBasic.Devices.Network.Ping(String hostNameOrAddress, Int32 timeout)

           at Microsoft.VisualBasic.Devices.Network.Ping(String hostNameOrAddress)

           at beta2TestProject.Form1.Button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\Jason H.JASON\My Documents\Visual Studio\Projects\beta2TestProjectBETA2\beta2TestProject\Form1.vb:line 7

           at System.Windows.Forms.Control.OnClick(EventArgs e)

           at System.Windows.Forms.Button.OnClick(EventArgs e)

           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)

           at System.Windows.Forms.Control.WndProc(Message& m)

           at System.Windows.Forms.ButtonBase.WndProc(Message& m)

           at System.Windows.Forms.Button.WndProc(Message& m)

           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)

           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)

           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)

           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)

           at System.Windows.Forms.Application.Run(ApplicationContext context)

           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()

           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()

           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)

           at beta2TestProject.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 76

           at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)

           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

           at System.Threading.ThreadHelper.ThreadStart()
    ----------------------------------------------------------


    Maybe I could send you the project and see if you can find why it simply will not return "False" if the site is not available. That is all I need it to do it return "True" or "False". Thanks Alot :)


         Jason

    Monday, May 23, 2005 2:07 AM
  • Hi Jason,

    Thanks for enclosing the Stack Trace.

    Unfortunately, there is no way to tell Ping() in My.Computer or System.Net to always return true or false for the reasons stated above.  If you want to write a little wrapper function that will have this behavior, you might try:


    ''' <summary>
    ''' Returns true if the destination is reachable via ping
    ''' </summary>
    Public Function IsDestinationReachable(ByVal hostnameOrAddress As String)
       Dim reachable As Boolean = False
       Try

          reachable = My.Computer.Network.IsAvailable AndAlso _
                           My.Computer.Network.Ping(hostnameOrAddress)

       Catch pingException As System.Net.NetworkInformation.PingException
       Catch genericNetworkException As _ System.Net.NetworkInformation.NetworkInformationException

          ' Fail silently and return false
       
    End Try
       Return reachable
    End Function

     



    You also might want to consider an alternative means of determining if the destination is reachable, as Ping really is not a reliable mechanism for determining if a host is available.  Because ping servers are a security concern, an increasing number of system administrators turn off the ping port on desktops.  Similarly, most firewalls only allow inbound HTTP (Port 80 or 8080) connections.  So you may find that the above function returns true, but the destination is actually reachable in most scenarios.

    If you're currently using Ping() to determine if a resource is available (e.g., a file or web page), then you might consider checking to see if you get an HTTP response from the server, downloading a well-known empty file on the server, etc.  These techniques, though a bit harder to implement, are much more robust.

    I hope this helps!

    Joe
    The VB Team

    Thursday, June 02, 2005 7:52 PM
  •  Minor typo...
    So you may find that the above function returns true, but the destination is actually reachable in most scenarios.

    Should be...
    So you may find that the above function returns FALSE, but the destination is actually reachable in most scenarios.
    Thursday, June 02, 2005 7:55 PM
  • Thanks alot for your comments and help! I may go ahead and look into going another route like you said I should do. Thanks again :)



               Jason
    Tuesday, June 07, 2005 2:16 AM