CraigLazar
asked on
VB.net Ping class
Hi,
Can i please have some help with regards to ping a ip address or host name for example 10.0.0.1 or www.microsoft.com?
I need to create a simple class that i can pass it an ip address and it then returns the result. I would like to then thread it so i can do multiple pings. I am not to sure wha tthe difference is between using ping in asyncronous or syncronous
thanks
Can i please have some help with regards to ping a ip address or host name for example 10.0.0.1 or www.microsoft.com?
I need to create a simple class that i can pass it an ip address and it then returns the result. I would like to then thread it so i can do multiple pings. I am not to sure wha tthe difference is between using ping in asyncronous or syncronous
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx
ASKER
Hi,
thanks for the links, i have already tried those but battling to get them to work.
in vb.net
thanks for the links, i have already tried those but battling to get them to work.
in vb.net
ASKER
this is what i have at the moment, which is returning a time out for reply object
Button on a form
Dim meas As New PingExample
Dim xme(2) As String
xme(0) = "www.microsoft.com"
xme(1) = "www.google.com"
PingExample.Main(xme)
Class
Public Class PingExample
' args[0] can be an IPaddress or host name.
Public Shared Sub Main(ByVal args As String())
Dim pingSender As New Ping()
Dim options As New PingOptions()
' Use the default Ttl value which is 128,
' but change the fragmentation behavior.
options.DontFragment = True
' Create a buffer of 32 bytes of data to be transmitted.
Dim data As String = "aaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaa"
Dim buffer As Byte() = Encoding.ASCII.GetBytes(da ta)
Dim timeout As Integer = 120
Dim reply As PingReply = pingSender.Send(args(0), timeout, buffer, options)
If reply.Status = IPStatus.Success Then
Console.WriteLine("Address : {0}", reply.Address.ToString())
Console.WriteLine("RoundTr ip time: {0}", reply.RoundtripTime)
Console.WriteLine("Time to live: {0}", reply.Options.Ttl)
Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment )
Console.WriteLine("Buffer size: {0}", reply.Buffer.Length)
End If
End Sub
End Class
Button on a form
Dim meas As New PingExample
Dim xme(2) As String
xme(0) = "www.microsoft.com"
xme(1) = "www.google.com"
PingExample.Main(xme)
Class
Public Class PingExample
' args[0] can be an IPaddress or host name.
Public Shared Sub Main(ByVal args As String())
Dim pingSender As New Ping()
Dim options As New PingOptions()
' Use the default Ttl value which is 128,
' but change the fragmentation behavior.
options.DontFragment = True
' Create a buffer of 32 bytes of data to be transmitted.
Dim data As String = "aaaaaaaaaaaaaaaaaaaaaaaaa
Dim buffer As Byte() = Encoding.ASCII.GetBytes(da
Dim timeout As Integer = 120
Dim reply As PingReply = pingSender.Send(args(0), timeout, buffer, options)
If reply.Status = IPStatus.Success Then
Console.WriteLine("Address
Console.WriteLine("RoundTr
Console.WriteLine("Time to live: {0}", reply.Options.Ttl)
Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment
Console.WriteLine("Buffer size: {0}", reply.Buffer.Length)
End If
End Sub
End Class
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is my ping function.
Have a try...
Have a try...
Public Function PingRemoteHost(ByVal HostAddress As String, Byval Arguments) As String
Dim PingReply As String = String.Empty
Using pingProcess As New Process
Dim startInfo As New ProcessStartInfo("ping")
startInfo.RedirectStandardInput = True
startInfo.RedirectStandardOutput = True
startInfo.UseShellExecute = False
startInfo.CreateNoWindow = True
startInfo.WindowStyle = ProcessWindowStyle.Hidden
startInfo.Arguments = HostAddress & " " & Arguments
pingProcess.StartInfo = startInfo
pingProcess.Start()
PingReply = pingProcess.StandardOutput.ReadToEnd
pingProcess.WaitForExit()
pingProcess.Close()
End Using
Return PingReply
End Function
Hang on... I think lenordiste has nailed it.... The microsoft web site (and countless many others) filter ping requests at the firewall. So, there's nothing wrong with your code.
...uh well, perhaps other than the fact you pass an array and only perform a ping on the first element in the array.
...uh well, perhaps other than the fact you pass an array and only perform a ping on the first element in the array.
ASKER
thanks guys for the help