Hi Leythos!
I have a VB6 program that lets me configure an INI file with IP
addresses to ping, another INI file which lets me setup and email
contact and SMTP server, and will count ping failures and email me when
a certain fault count is reached in succession.
I need to convert the app to VB.Net and was looking on-line for source
code examples, I found one example, but would like to see other methods
to do the Ping and get the result.
Anyone got a link to source code for VB.Net ping examples?
You can use this code:
Imports System
Imports System.Collections
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class Ping
Dim _hostAdd As IpAddress
Dim _timeoutMs As Integer
Dim _lastError As String
Dim _resolvedIpAddress As String
Public Sub New()
_timeoutMs = 2000000
End Sub
Public Sub New(ByVal timeoutSec As Integer)
_timeoutMs = timeoutSec * 1000
End Sub
Public ReadOnly Property LastError() As String
Get
Return _lastError
End Get
End Property
Public ReadOnly Property IpAddress() As String
Get
Return _resolvedIpAddress
End Get
End Property
Public Function CheckByNameOrIP(ByVal vw_name As String) As Boolean
Try
_hostAdd = Dns.Resolve(vw_name).AddressList(0)
Catch ex As SocketException
_lastError = ex.Message
Return False
End Try
Return LocalPing()
End Function
Public Function CheckByIpAddr(ByVal ipAddr As String) As Boolean
Try
_hostAdd = System.Net.IPAddress.Parse(ipAddr)
Catch ex As SocketException
_lastError = ex.Message
Return False
End Try
Return LocalPing()
End Function
Private Function LocalPing() As Boolean
Const SEND_SIZE As Integer = 16
Const RECEIVE_SIZE As Integer = 200
Const ECHO_PORT As Integer = 7
_resolvedIpAddress = _hostAdd.ToString()
Dim sendBytes(SEND_SIZE) As Byte
Dim recvBytes(RECEIVE_SIZE) As Byte
Dim rcvd As Integer
Dim EPhost As IPEndPoint
Dim sent As Integer
Try
EPhost = New IPEndPoint(_hostAdd, ECHO_PORT)
Dim s As Socket = New Socket( _
AddressFamily.InterNetwork, _
SocketType.Raw, ProtocolType.Icmp)
sendBytes(0) = 8
sendBytes(1) = 0
sendBytes(2) = &HF7
sendBytes(3) = &HFF
sendBytes(4) = 0
sendBytes(5) = 0
sendBytes(6) = 0
sendBytes(7) = 0
sendBytes(8) = 0
sendBytes(9) = 0
sendBytes(10) = 0
sendBytes(11) = 0
sendBytes(12) = 0
sendBytes(13) = 0
sendBytes(14) = 0
sendBytes(15) = 0
sent = s.SendTo(sendBytes, SEND_SIZE, SocketFlags.None, EPhost)
Dim cr As ArrayList = New ArrayList
cr.Add(s)
Socket.Select(cr, Nothing, Nothing, _timeoutMs)
If cr.Count > 0 Then
rcvd = s.Receive(recvBytes, RECEIVE_SIZE, SocketFlags.None)
_lastError = ""
Return True
End If
Catch ex As SocketException
_lastError = ex.Message
Return False
End Try
_lastError = "Timeout trying to reach the remote host"
Return False
End Function
End Class
Usage is easy:
Dim p As Ping = New Ping
MessageBox.Show(p.CheckByIpAddr("127.0.0.1").ToString())
MessageBox.Show(p.CheckByNameOrIP("
www.dfgdfgdfgdgdf.de").ToString())
Another possibility (if you are running an XP machine) is using WMI:
' Add a reference to System.Management.dll
Imports System.Management
Private Function Ping(ByVal IP As String) As Boolean
Dim mos As ManagementObjectSearcher = _
New ManagementObjectSearcher( _
("SELECT * FROM Win32_PingStatus " & _
"WHERE Address = '" & IP & "'"))
Dim moc As ManagementObjectCollection = _
mos.Get
Dim mo As ManagementObject
For Each mo In moc
If Not mo("StatusCode") Is Nothing Then
If Int32.Parse(mo("StatusCode").ToString()) = 0 Then
Debug.WriteLine(mo("ProtocolAddress"))
Debug.WriteLine(mo("BufferSize"))
Debug.WriteLine(mo("ResponseTime"))
Debug.WriteLine(mo("ResponseTimeToLive"))
Return True
End If
Else
Debug.WriteLine("Does not respond")
Return False
End If
Next
End Function
Last but not least you can use "ping.exe" and grab the output:
Imports System
Imports System.Diagnostics
Imports System.Text.RegularExpressions
Public Class Ping2
Public Shared Function PingHost(ByVal host As String) As Integer
Dim proc As Process = New Process
proc.StartInfo.UseShellExecute = False
proc.StartInfo.CreateNoWindow = True
proc.StartInfo.FileName = "ping"
proc.StartInfo.Arguments = "-n 1 " + host
proc.StartInfo.RedirectStandardOutput = True
proc.Start()
proc.WaitForExit()
Dim output As String = proc.StandardOutput.ReadToEnd()
'I have used this on a german machine
'You'll have to adopt the pattern
Dim pattern As String = "Mittelwert = (\d*)ms"
'if host is responding
'return response time
If Regex.IsMatch(output, pattern) Then
Dim m As Match = Regex.Match(output, pattern)
Return Integer.Parse(m.Groups(1).Value)
Else
Return -1
End If
End Function
End Class
Uhhh, I think this has been comprehensive....
Cheers
Arne Janning