Results 1 to 7 of 7

Thread: Internet Connectivity - Ping-then-do

  1. #1

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Internet Connectivity - Ping-then-do

    The attached project has code (MonitorNetworkResource) that can be used to:

    - Monitor devices on your network
    - Monitor Internet connectivity - it does differently than other tools.

    As you may or may not know I think pinging "something" to determine whether or not to do "something" is wrong. An example would be ping a mail server, if the ping is good then send the mail. At best it is based on a naive assumption. If you want to discuss it or have an example of when it is better, I'll be glad to. (see Post #3 this Thread) The complex example shows most of the features of MonitorNetworkResource , though it is unlikely anyone would use them all at once. MonitorNetworkResource was created for developers and should be used as an alternative to ping-then-do.


    Here is a simplified drawing of what the internet looks like.




    When you ping some far distant host, say www.yahoo.com, and get successful replies the internet is up. When unsuccessful is it Yahoo, your ISP, or some other ISP along the route that is down? Regardless of which, the network is down as far as you are concerned.

    More importantly do you care if it is beyond your ISP? You can't do anything about it! I think what most of us really want is to know that we can get to our ISP.

    The code references the “edge”. It is the point believed to be where you encounter the internet. In the picture these edges are shown as red dots. (note: PP = peering point) I am the PC attached to RadioWire, and my link IS WIFI, and in reality I am several radio links further away than denoted, which is why my Miles to Edge is off(I use 90% of C for the calculation).

    The code determines where the “edge” of the internet is by initially doing a series of pings and checking the IP address of the reply(I am using the same mechanism as trace route).

    The “edge” is found when the reply address is not:

    On your network
    A private IP address (RFC 1918)

    Exceptions:

    - if the device is on your network and replies successfully, which is why this can be used to monitor devices on your net
    - if the reply address is automatic private (169.x.x.x).

    From that point on a ping is sent to the “edge” periodically. If you can get to the "edge", then your internet connection is deemed to be up.

    I have used similar code in place of pinging some site on the web for a long time. The code contains the accumulation of all that I have learned. Here is a screen shot of the complex sample application. I have simulated a network outage by disconnecting the wire between my Router and the ISP.




    A good test of the sample app is:

    1 - Start the application

    2 – While the app is running repair your network connection if you have a DHCP assigned address, or disable / enable your network interface.

    You should see several transitions in the app.

    So give it a try, let me know what you think.

    Current Code

    MonitorNetworkResource dll source always here - Needed for all of the sample applications.

    DBStuff.zip


    Simple Sample Application always here

    BasicMonitor.zip


    Complex Sample Application always here

    ToPingOrNot.zip



    Note - Do not hook NetworkAvailabilityChanged (one of the events available in MyApplication Events). This event is hooked by MonitorNetworkResource. You can gain access to this event by handling NetStateChange which is raised by MonitorNetworkResource.


    Note: Do not use this code in commercial products without my permission.

    Update Info - check back often

    Updated - 27 Mar 2010 @ 1220 CST - uncovered error thanks to Frank L Smith @ MSDN Visual Basic Forum. Initial hop errors cause down indication. Code now keeps hopping until it reaches a max of 5. Situations like this
    Code:
      1    <1 ms  4294967283 ms  4294967283 ms  192.168.1.1
      2     *        *        *     Request timed out.
      3  4294967290 ms  4294967292 ms  4294967292 ms  68.86.151.181
      4     8 ms    40 ms     8 ms  68.85.174.89
      5  4294967292 ms  4294967292 ms  4294967293 ms  68.85.174.85
    should now work.

    Updated - 29 Mar 2010 @ 1902 CST - Post #6 has list of available methods / properties.

    Updated - 30 Mar 2010 @ 0713 CST - Because there are three demo app's now I became tired of copy / pasting MonitorNetworkResource. MonitorNetworkResource has been moved to a .dll project and will have to be built separately. The reference in the demos will need to be changed to where you create your version of the dll

    Updated - 31 Mar 2010 @ 1148 CST - Last night I added another router to my test net. In the process I found out that, IMHO, they do not process ICMP packets exactly correctly. I think I'll just leave it at that. The DLL was updated to handle the situation.
    Last edited by dbasnett; Mar 31st, 2010 at 11:54 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  2. #2

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Internet Connectivity - Ping-then-do

    Installed the sample application on Windows 7 without problem.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Internet Connectivity - Ping-then-do

    Why "Ping-then-Do" doesn't make sense

    You see this all the time:

    "I want to ping my (web, mail, ftp) server before I (browse, send mail, transfer file) to make sure that the server is up."

    Sounds reasonable, but the logic is flawed because it is based on a false assumption.

    Your assumption is that if you send a ping and it is successful, the network will be up when you perform the action you really wanted to. You also assume the inverse. If I send a ping and it fails, the action I want to perform will fail.

    In pseudo-code it looks like this:

    Code:
      If ping (www.yahoo.com) is successful then
        Try
          Send Mail
        Catch
          Give user feedback
        End EndTry
      End If
    What you seem to not comprehend is that networks change state all the time. For example, I know for a fact that if I ping Yahoo, and then browse to Yahoo, the responses can, and often do, come from different servers. I also know for a fact that if you ping www.vbforums.com it will fail, but it doesn’t mean the server is down. I also know that between the time you send your ping and you start to send the mail the network may go down.

    So in pseudo-code what it should look like is this:

    Code:
      Try
        Send Mail
      Catch
        Give user feedback
      End EndTry
    The other argument I here for "Ping-then-Do" is that why start some lengthy process, i.e. FTP a large file, if the server is down. Once again the underlying assumption is false. No matter how large the file, it is presented to the network as a series of small chunks. Here is a capture of an FTP:





    As you can see there are a lot of things that happen before the “large” thing you are trying to avoid actually happens. The actual start of the process is no more load on the network than a ping would be. Other services, such as mail, have similar small overhead “startup” packets.

    The bottom line, and I apologize if this sounds rude, is that if you are going to write programs that use the network, you should at least have a basic idea of the fundamentals. The desire to "Ping-then-Do" tells me that many who are, don’t. A frequent poster, Frank L Smith @ MSDN, said "...performing a ping prior to an upload or download is feckless; that state is only true at the very instant in time so it's of no real value."

    "Ping-then-Do" clogs up the backbone with packets that serve no purpose. It is one of the reasons that many ISP's block ICMP responses. The use of MonitorNetworkResource could be a good thing for ISP's. The last time I looked 2&#37; of the traffic on the backbone was ICMP.

    In post #1 I have offered a compromise. It does ping, but limits the scope of the ping to your edge of the internet.
    Last edited by dbasnett; Mar 31st, 2010 at 07:06 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Internet Connectivity - Ping-then-do

    Ping / Trace Route

    Ping

    Ping is a computer network administration utility that is typically used to test whether a particular host will respond to the ping, and if so, how long it took the ping to make the round trip. Ping uses the Internet Control Message Protocol (ICMP - RFC 792) echo request packets to the target host and waits for an ICMP response.


    Trace Route

    Trace route is nothing more than a Ping that manipulates the Time To Live(TTL) field in the ICMP packet. Normally packets are sent with the TTL field set to a number that is equal to or greater than the number of routers it will transit. Each router that forwards a packet decrements the TTL field and if it is greater than zero the packet is forwarded. If not, the packet is returned with an error indicating that the TTL has expired.

    So lets look at an example network:

    YourPC ---- YourRouter ---- NextRouter ---- Server

    In this example there are 2 hops between YourPC and Server.

    A trace route from YourPC to Server:

    1 - Trace route sets the TTL to 1 and sends a ping to Server. YourRouter gets the packet, decrements the TTL field, sees it is 0 and returns the packet to YourPC . YourPC gets the response from YourRouter , notes the round trip time (RTT), and who (YourRouter ) sent the packet.

    2 - Trace route sets the TTL to 2 and sends a ping to Server. YourRouter gets the packet, decrements the TTL field, sees it is > 0 and forwards the packet to NextRouter. NextRouter gets the packet, decrements the TTL field, sees it is 0 and returns the packet to YourPC . YourPC gets the response from NextRouter , notes the RTT, and who (NextRouter ) sent the packet.

    3 - Trace route sets the TTL to 3 and sends a ping to Server. YourRouter gets the packet, decrements the TTL field, sees it is > 0 and forwards the packet to NextRouter. NextRouter gets the packet, decrements the TTL field, sees it is > 0 and forwards the packet to Server. Server responds to YourPC normally.

    Here is what that might look like:

    Code:
    TTL    Response Times          
      1     1 ms     1 ms     1 ms  YourRouter 
      2     2 ms     1 ms     1 ms  NextRouter
      3     5 ms     5 ms     1 ms  Server
    My example trace route only sent 1 ping for each TTL setting. Most trace route programs send multiple pings for each TTL setting, which is why you see three times above.
    Last edited by dbasnett; Mar 29th, 2010 at 04:32 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Internet Connectivity - Web Demo

    Attached is a web browser demo that uses MonitorNetworkResource.

    WebDemo.zip
    Last edited by dbasnett; Mar 30th, 2010 at 07:29 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Internet Connectivity - Ping-then-do

    Short description of the exposed properties / methods.

    Code:
    Public Class MonitorNetworkResource
    
    MonitorNetworkResource hooks NetworkAvailabilityChanged (one of the events available in MyApplication Events).
    This event is raised by MonitorNetworkResource and can be handled by your application using this code.
    This code can be called by different threads which is why it is code this way.
    
        'UI event handler for  Event NetStateChange(ByVal UpDown As Boolean)
    
        Delegate Sub myStub(ByVal UpDown As Boolean)
        Public myDelegate As myStub
    
        Private Sub CaptureNetStateChangeEvent(ByVal UpDown As Boolean) _
                Handles MyMonitor.NetStateChange
            If Me.InvokeRequired Then
                myDelegate = New myStub(AddressOf CaptureNetStateChangeEvent)
                Me.Invoke(myDelegate, UpDown)
                Exit Sub
            End If
    
            If UpDown Then
                'net up event
                'your code here
            Else
                'net down event
                'your code here
            End If
        End Sub
    
    
    
    If New is called without a resource then the internet is assumed.  StartNow, if true, 
    starts monitoring immediately, and a call to Start is not required.
    
            Sub New(Optional ByVal ResourceToMonitor As String = "", _
                           Optional ByVal StartNow As Boolean = False)
    
    
    Start monitoring. 
    
            Sub Start()
    
    
    Sop monitoring.
    
            Sub StopMonitor()
    
    The intended usage is to start the monitor and periodically check the availability of the
    resource monitored.  Starting and stopping repeatedly defeats the intended purpose.
    
    
    Force an update of the status.
    
            Sub CheckNow()
    
    
    If you request a resource and it is available before the internet is encountered, this will return True.
    
            ReadOnly Property isRequestedResource() As Boolean
    
    
    Returns true if DNS appears able to resolve names.
    
            ReadOnly Property isReady() As Boolean
    
    
    Returns the state of the resource being monitored
    
            ReadOnly Property isUp() As Boolean
    
    
    Returns what is being used as a target for the pings.  This may or may not be what is actually monitored.(see isRequestedResource)
    
            ReadOnly Property Target() As String
    
    
    How often is the resource being pinged.
    
            ReadOnly Property Interval() As Integer 'return how often pings happen
    
    
    Set how often the resource is to be pinged.  (min 15 – max 300)
    
            Function SetMonitorInterval(ByVal SecondsBetweenPings As Integer) As Boolean
    
    
    Set ping timeout.  Default is 250 ms.  Min is 10 ms.  Max is 4 times the time it takes light to travel to the moon and back.
    
            Function SetPingTimeOut(ByVal PingTimeoutIn_ms As Integer) As Boolean
    
    
    This will return the gateway being used to access the resource.  It should work with machines with multiple interfaces, but has not been tested.
    
            ReadOnly Property GetGateway() As String
    
    
    Get the ip address of the monitored resource.
    
            ReadOnly Property GetIP_Monitored() As String
    
    
    
    Get the average RTT.
    
            ReadOnly Property GetAverageRTT() As Long
    
    
    Get the state of the adapter.
    
            ReadOnly Property GetAdapterState() As Boolean
    
    
    Get the number of hops to the actual resource being monitored.
    
            ReadOnly Property GetHopCount() As Integer
    
    
    Gets the count of good hops to the requested resource.  If the resource is up will be the same as GetHopCount.
    
            ReadOnly Property GetHopGood() As Integer
    
    
    Get the count of the actual number of pings sent.
    
            ReadOnly Property GetPingsSent() As Long
    
    
    A guesstimate of the distance in miles to the resource being monitored.  90% of C is used for this calculation.  1 AU(92955807) is returned as an error.
    
            ReadOnly Property GetDistanceInMiles() As Double
    
    
    Get all of the internal status indications, optionally in binary.
    
            ReadOnly Property GetAllStatus(Optional ByVal AsBinary As Boolean = False) As String
    
    
    Get the TOD the last ping was sent.
    
            ReadOnly Property GetTimeLastPingSent() As DateTime
    
    
    Get DNS errors.
    
            ReadOnly Property GetErrorsPingDNS() As String
    
    
    Get a count of the total number of times the requested resource has been down.
    
            ReadOnly Property GetDownCount() As Integer
    
    
    Get total time the requested resource has been down.
    
            ReadOnly Property GetDownTime() As String
    
    
    The following returns the IP address associated with the interface that is being used.
    
            ReadOnly Property GetMyIP() As String
    
    
    Get a list of the last 100 events (Up / Down)
    
            ReadOnly Property GetEvents() As List(Of String)
    In addition some general interface information can be found here:

    Code:
            Public myInterfaces As New AllInterfacesClass
    Last edited by dbasnett; Mar 29th, 2010 at 07:00 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Re: Internet Connectivity - Ping-then-do

    This is a year old response but I don't care. Frankly, this is a well thought out topic on your part dbasnett with multiple references and ideas to back it up that are sound. Bravo.

Tags for this Thread

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