How to Ping a machine programmatically using Windows Forms
Posted by: Suprotim Agarwal ,
on 6/1/2008,
in
Category WinForms & WinRT
Abstract: The ‘Ping’ class in the System.Net.NetworkInformation namespace provides functionality to determine if a remote machine can be accessed over a network. In this article, we will use the methods and properties of the ‘Ping’ class to determine availability of a machine.
How to Ping a machine programmatically using Windows Forms
The ‘Ping’ class in the System.Net.NetworkInformation namespace provides functionality to determine if a remote machine can be accessed over a network. In this article, we will use the methods and properties of the ‘Ping’ class to determine availability of a machine.
The ‘Ping’ class is similar to the Ping.exe tool. You can send an Internet Control Message Protocol (ICMP) echo request using both synchronous and asynchronous methods of the ‘Ping’ class. The Send() method is a synchronous method and returns a ‘PingReply’ containing the results of an ICMP request. It blocks the application while waiting for a reply. The SendAsync() is an asynchronous method which uses a separate thread to send the request. The ‘PingCompleted’ event is raised when the operation completes and you can use the ‘PingCompletedEventHandler’ delegate to handle it.
Let us see how to use these classes to send a request both synchronously and asynchronously:
Use the namespace System.Net.NetworkInformation
C#
using System.Net.NetworkInformation;
VB.NET
Imports System.Net.NetworkInformation
Using the Send() method:
C#
string mac = "www.dotnetcurry.com"; // use any other machine name
Ping pingreq = new Ping();
PingReply rep = pingreq.Send(mac);
Console.WriteLine("Pinging {0} [{1}]", mac, rep.Address.ToString());
Console.WriteLine("Reply From {0} : time={1} TTL={2}",
rep.Address.ToString(), rep.RoundtripTime, rep.Options.Ttl );
Console.ReadLine();
VB.NET
Dim mac As String = "www.dotnetcurry.com" ' use any other machine name
Dim pingreq As Ping = New Ping()
Dim rep As PingReply = pingreq.Send(mac)
Console.WriteLine("Pinging {0} [{1}]", mac, rep.Address.ToString())
Console.WriteLine("Reply From {0} : time={1} TTL={2}", rep.Address.ToString(), rep.RoundtripTime, rep.Options.Ttl)
Console.ReadLine()
Using the SendAsync() method:
C#
System.Threading.AutoResetEvent autores = new System.Threading.AutoResetEvent(false);
string mac = "www.dotnetcurry.com";
Ping pingasync = new Ping();
pingasync.PingCompleted += new PingCompletedEventHandler(PingCompletedMethod);
pingasync.SendAsync(mac, "ping");
autores.WaitOne();
private static void PingCompletedMethod(object sender, PingCompletedEventArgs e)
{
PingReply rep = e.Reply;
Console.WriteLine("Pinging {0}", rep.Address.ToString());
Console.WriteLine("Reply From {0} : time={1} TTL={2}",
rep.Address.ToString(), rep.RoundtripTime, rep.Options.Ttl);
Console.ReadLine();
if(e.Error != null)
{
Console.WriteLine("Ping was not successful");
Console.WriteLine(e.Error.ToString());
}
}
VB.NET
Private autores As System.Threading.AutoResetEvent = New System.Threading.AutoResetEvent(False)
Private mac As String = "www.dotnetcurry.com"
Private pingasync As Ping = New Ping()
Private pingasync.PingCompleted += New PingCompletedEventHandler(PingCompletedMethod)
pingasync.SendAsync(mac, "ping")
autores.WaitOne()
private static void PingCompletedMethod(Object sender, PingCompletedEventArgs e)
Dim rep As PingReply = e.Reply
Console.WriteLine("Pinging {0}", rep.Address.ToString())
Console.WriteLine("Reply From {0} : time={1} TTL={2}", rep.Address.ToString(), rep.RoundtripTime, rep.Options.Ttl)
Console.ReadLine()
If Not e.Error Is Nothing Then
Console.WriteLine("Ping was not successful")
Console.WriteLine(e.Error.ToString())
End If
So that was the Ping class for you. Just remember that some firewalls block the ICMP requests. So make sure to allow ICMP requests before trying this sample on your machines. I hope this article was useful and I thank you for viewing it.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn