Debate: To Ping or Not To Ping?

…that is the question!

An interesting thread over on vbforums.com brought up the topic of using a ping to condition whether or not to continue performing a network task. One argument is that you want to see if there is a viable connection to the remote device before you try to do anything to/with it. The other argument is, why introduce the bandwidth overhead of a ping command when you can just attempt the task and handle the exception if it fails.

Both interesting perspectives, and personally, I am in the boat that says ping first, task next. I can understand both sides though and have posted this for both sides to discuss.

EDIT: Here is a short code snippet to test the pre-ping theory. Code is provided by @dbasnett and is a great example of the time involved with a ping request.

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("hh@df.do", "dfdf@dfdf.com", "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("hh@df.do", "dfdf@dfdf.com", "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

18 thoughts on “Debate: To Ping or Not To Ping?

  1. I think the answer to this varies from situation to situation… in some scenarios you might want to just ping the server before attempting to send a load of data to it, but I think in the majority of cases it is not really worth doing. However, because a ping is such a small amount of data and is a quick thing to do, I don’t think it really causes any harm doing it.

    You just need to be aware that if ICMP traffic is blocked then the ping is going to fail even if the server is online and accepting data on other ports, so if you cant always be certain that the ping will get through then don’t do it as you might end up not performing your desired action even though the server is online. I know others will disagree and say that there is no point in doing a ping in ANY situation but that is just my opinion 🙂

  2. 1 – Pings have a purpose if you are a Network Admin.
    2 – Basing an application on the model of Ping before Doing (mail,web,ftp,etc.) is ridiculous and demonstrates that the person doing the coding doesn’t understand network basics.
    3 – Even if ICMP works today, and you base your code on Ping-then-do, what happens if tomorrow ICMP doesn’t work?

    Before deciding to go somewhere in your automobile do you run outside and start it, or do you decide to go somewhere, go outside, start the car and go, if it starts.

    I am still waiting for an example of where it(ping before do) actually accomplishes something.

  3. What happens if tomorrow FTP or SMTP doesn’t work? 🙂

    I think I am beginning to side more with the “ping-then-do” (i like that wording) when it is specifically for net administration-type tools where a status of a device is needed or to see if something exists at a certain IP (again, this may not always work with a ping)

    Sometimes I go outside, start my car, then run back inside. 1. because I baby my car and ALWAYS let it warm up first and 2. because it’s cold outside and/or I forgot something 🙂

  4. What happens tomorrow if they start charging for actual bytes transmitted?

    But hadn’t you already decided to go? 🙂

    I have been in charge on some very large networks, and have been around networking since before Cisco, and have looked at more packet traces than I care to remember, and Ping was a good friend. I don’t think what we have been talking about is that.

    BTW – why does this blog strip out blank lines?

  5. Excluding network admin uses, what is the purpose of ping-then-do?

    Assuming the target of the ping is up and responds successfully:

    Is ping-then-do
    1. Faster
    2. Less bandwidth
    3. preventing failures occurring after the do starts
    4. gives me a warm fuzzy and what I have always done

    All I have heard is 4.

  6. The other side of post 7 is
    Assuming the target of the ping is down:
    1.  Does it mean that my “Do” won’t work?
    2.  That ping is the only way to provide useful feedback?
    3.  That a ping fails “faster” than the “Do”?
     

  7. I don’t care, but that whole argument made no sense, 0, none, nada.  I hope that every one knows that a check for a condition in an application is faster than a ping.

  8. It looks like the opposing view is, 4. gives me a warm fuzzy and what I have always done.  It is hard to debate feelings.  I’ll check back periodically to see if we are having a factual debate, or feelings.
    @Steve – did you see my comments on San Antonio at VBForums?

  9. I think that even if you use the ping-then-do method you still need to have an efficient exception handling, making the ping useless. If you didn’t have any exception handling then the ping would be usefull but the code would be awfull :P.
    For that I think that using exception handling is better than using the ping.

  10. I think the perceived goal is to prevent doing a “labor”-intensive network task by checking the availability of the “other end” first. I think dbasnett has convinced me that, with the exception of network admin tools (monitoring devices), there really is no need to “ping-then-do.”

    Like you said, you’ll be exception handling anyway, so you might as well just go ahead and do the task, and handle whatever exceptions may happen.

  11. Just stumbled across this and was curious about something I was asking about in the VBforums. Doesn’t ping provide some time savings in some cases? In my program I’m executing a stored procedure on a remote server. If I execute the code in a try/catch block on it’s own it can take as long as 40 seconds to fail if the network is unavailable or the computer running the SQL server is offline for some reason. If the destination computer is on and connected then the SQLcommand and SQLconnection timeout properties take over and there’s no problem.

    So isn’t there a time advantage to using ping first, since I can set the timeout period, to determine if the path to the server is available?

Leave a Reply

Your email address will not be published. Required fields are marked *