PDA

Click to See Complete Forum and Search --> : To Ping or Not to Ping - Check Internet Question


dbasnett
Jan 31st, 2010, 10:41 AM
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.

'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

chris128
Jan 31st, 2010, 11:19 AM
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?

dbasnett
Jan 31st, 2010, 11:27 AM
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.

chris128
Jan 31st, 2010, 11:31 AM
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.

dbasnett
Jan 31st, 2010, 11:39 AM
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.

dbasnett
Jan 31st, 2010, 12:14 PM
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

dbasnett
Jan 31st, 2010, 01:59 PM
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.

dbasnett
Feb 1st, 2010, 09:50 AM
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.