Results 1 to 8 of 8

Thread: To Ping or Not to Ping - Check Internet Question

  1. #1

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

    To Ping or Not to Ping - Check Internet Question

    This is not for Net Admins that have a legitimate use for ping.

    Many times I have seen posters ask the question of how can they check that the internet is up before (HTTP, Mail, FTP, etc.). And often the answer, and I have been guilty of this, is to tell the poster to ping in some form.

    My contention is that the "correct" answer is:

    "Do or do not…there is no try!"

    Instead of checking, just perform the action (web request, mail, ftp, etc.) and be prepared for the request to fail, which you have to do anyway, even if your check was successful.

    Consider the following:

    1 - check, and it is OK
    2 - start to perform action
    3 - network goes down
    4 - action fails
    5 - lot of good your check did

    If the network is down your action will fail just as rapidly as a ping, etc.

    1 - start to perform action
    2 - if the net is down(or goes down) the action will fail

    If you have a need for a warm fuzzy that you think ping provides you might try something like this.

    Code:
    'sample code 
        Private Sub Button1_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles Button1.Click
            Dim s As String = DoGetHostEntry(TextBox1.Text)
            If s <> "" Then
                'success - do your mail, web, file stuff here
                TextBox2.Text = s
                Try
                    Dim HttpWReq As System.Net.HttpWebRequest = _
                               CType(System.Net.WebRequest.Create("http://www.bar.foo"), System.Net.HttpWebRequest)
    
                    Dim HttpWResp As System.Net.HttpWebResponse = _
                       CType(HttpWReq.GetResponse(), System.Net.HttpWebResponse)
                Catch ex As Exception
                    TextBox2.Text = ex.Message
                    flushDNS() 'if failed, cause all subsequent DNS requests to resolve
                End Try
            Else
                'failure
                TextBox2.Text = "Net down, bad name, dns down"
            End If
        End Sub
        'end sample
    
        Public Function DoGetHostEntry(ByVal hostName As String) As String
            'DNS searches the local cache first.  The following
            'talks about how long entries are stored in local cache.
            '
            'http://www.helpwithwindows.com/WindowsXP/tune-24.html
            '
            'If you decide to change the value, use common sense please.
            '
            Dim host As System.Net.IPHostEntry = Nothing
            Dim success As Boolean = False
            Dim retval As String = ""
            Try
                host = System.Net.Dns.GetHostEntry(hostName)
                success = True
            Catch ex As Exception
                flushDNS() 'if failed, cause all subsequent DNS requests to resolve
            Finally
                If success Then
                    Dim ip As System.Net.IPAddress() = host.AddressList
                    If ip.Length <= 0 Then
                        flushDNS() 'if failed, cause all subsequent DNS requests to resolve
                    Else
                        retval = ip(0).ToString
                    End If
                End If
            End Try
            Return retval
            '
        End Function
    
        Private Sub flushDNS()
            'Use some common sense about flushing the local DNS cache!
            'to test if this will work
            'Start / Run -  ipconfig /displaydns
            'or
            '               ipconfig /flushdns
            Dim pi As New ProcessStartInfo
            pi.FileName = "ipconfig.exe"
            pi.Arguments = "/flushdns"
            pi.CreateNoWindow = True
            Dim p As New Process
            p.StartInfo = pi
            p.Start()
        End Sub
    Last edited by dbasnett; Feb 1st, 2010 at 10:53 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
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: To Ping or Not to Ping - Check Internet Question

    OK well I'm gonna say the same thing I said in this thread http://www.vbforums.com/showthread.php?t=601584 - How is a DNS lookup an alternative to a ping?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

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

    Re: To Ping or Not to Ping - Check Internet Question

    1. If the site has not been visited, it will require a DNS look-up. If you receive an answer then(assuming your DNS is not local) you have an indication that the internet is up. This DNS look-up has to occur no matter what caused it.

    2. If the site has been visited and you perform your action, and it fails, the code flushes the DNS. This results in DNS look-ups thereafter. See #1.
    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
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: To Ping or Not to Ping - Check Internet Question

    OK but that only applies to checking the internet is available - the original thread that sparked this discussion (the one I linked to in previous post) was not about checking to see if the internet is up, it was simply about pinging the internal email server before attempting to send an email to it using SmtpClient.

    Aside from that, unless you are a home user then its pretty likely that the network will have its own DNS server and this will have cached DNS records of its own so clearing your own DNS cache does not necessarily mean that you will get a 'live' response to a DNS query as the local DNS server could just return its own cached record. I dont think a DNS lookup is a very good way of confirming internet connectivity and I would say a ping or HTTP request is better for that.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

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

    Re: To Ping or Not to Ping - Check Internet Question

    Quote Originally Posted by chris128 View Post
    OK but that only applies to checking the internet is available - the original thread that sparked this discussion (the one I linked to in previous post) was not about checking to see if the internet is up, it was simply about pinging the internal email server before attempting to send an email to it using SmtpClient.

    Aside from that, unless you are a home user then its pretty likely that the network will have its own DNS server and this will have cached DNS records of its own so clearing your own DNS cache does not necessarily mean that you will get a 'live' response to a DNS query as the local DNS server could just return its own cached record. I dont think a DNS lookup is a very good way of confirming internet connectivity and I would say a ping or HTTP request is better for that.
    There was a big IF in what I said.

    "...I would say a ping or HTTP request is better for that."

    My real point is that whatever your real work is, is the best check. No one has shown a scenario where doing a ping actually accomplishes something better than doing what you REALLY want in the first place.
    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: To Ping or Not to Ping - Check Internet Question

    Code:
        Dim srvrName As String = "mail.foo.bar"
        Dim smtpserver As New Net.Mail.SmtpClient(srvrName)
        Dim mailMessage As System.Net.Mail.MailMessage
        Private Sub foobar()
    
            'The argument for PING
            Try
                If My.Computer.Network.Ping(srvrName) Then
                    smtpserver.Send("[email protected]", "[email protected]", "jjj", "jdfo")
                    MessageBox.Show("Mail sent successfully")
                Else
                    MessageBox.Show("Could not ping someaddress")
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                'Exit Sub
            End Try
    
            'The opposing view - the one I support
            Try
                smtpserver.Send("[email protected]", "[email protected]", "jjj", "jdfo")
                MessageBox.Show("Mail sent successfully")
            Catch ex As Exception
                If Not ex.InnerException Is Nothing Then
                    MessageBox.Show(ex.InnerException.Message)
                Else
                    MessageBox.Show("Failure sending mail")
                End If
                'Exit Sub
            End Try
    
        End Sub
    Last edited by dbasnett; Feb 1st, 2010 at 10:44 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

  7. #7

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

    Re: To Ping or Not to Ping - Check Internet Question

    related discussion is here http://www.vbforums.com/showthread.php?t=601584 and here http://stateofidleness.com/?p=326

    If you are writing code and basing the decision on the success or failure or ICMP echo / response, then I contend you are not writing the best code you can.

    a) Do most people start their car and then decide to make a trip or b)do they decide to make a trip and find out the car wouldn't start? If it so happens that you have been having a lot of car troubles, you might select option b, but the real answer is fix the damn car so you can select option a.
    Last edited by dbasnett; Feb 1st, 2010 at 10:45 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

  8. #8

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

    Re: To Ping or Not to Ping - Check Internet Question

    I recently heard that another reason for a ping is that it provided the ability, and was easier, for clearer error messages than checking for an innerexception when doing smtp sendmails. I updated post #6 to reflect that. In the process I discovered that those arguing in favor of ping-then-do may not have thought of everything.
    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

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