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
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