VB.net Ping class

Posted on 2010-09-06
Medium Priority
Last Modified: 2012-05-10
Can i please have some help with regards to ping a ip address or host name for example 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

Question by:CraigLazar
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 23

Accepted Solution

Kamaraj Subramanian earned 800 total points
ID: 33612366
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 33612371

Author Comment

ID: 33612458
thanks for the links, i have already tried those but battling to get them to work.
in vb.net
Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)


Author Comment

ID: 33612499
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

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 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Dim buffer As Byte() = Encoding.ASCII.GetBytes(data)
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("RoundTrip 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
LVL 11

Assisted Solution

lenordiste earned 800 total points
ID: 33613491
everything is fine with your code, it's just that your test data is not: "www.microsoft.com" does not reply to the ping.

"www.google.com" does however so you can test this one:

Expert Comment

ID: 33614360
Here is my ping function.
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

            PingReply = pingProcess.StandardOutput.ReadToEnd


        End Using
        Return PingReply
End Function

Open in new window

LVL 41

Expert Comment

ID: 33617552
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.

Author Closing Comment

ID: 33635502
thanks guys for the help

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question