How to Ping a machine programmatically using Windows Forms

Posted by: Suprotim Agarwal , on 6/1/2008, in Category WinForms & WinRT
Views: 50857
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.
If you liked the article,  Subscribe to my RSS Feed. 
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+
Further Reading - Articles You May Like!
Author
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 a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by ilango on Thursday, August 7, 2008 10:49 AM
Hello
       I created web service using REST.i can run in single machine properly.it is fine.when i send request from one machine another machine using xmlHttpRequest("GET","https://192.168.1.2","false"); the following error occur.

Security Error: Content at https://192.168.1.79/rest/index.html may not load data from https://192.168.1.2?timestamp=1218114764816.

Please give solution
Comment posted by Matthew on Friday, May 21, 2010 9:06 PM
Java restrictions, to access the file from a remote computer you need a crossdomain.xml file
Comment posted by Jayesh Sorathia on Thursday, July 12, 2012 3:26 AM
This is very good example.
Visit this link to learn more about Ping Class. Click Here...
http://jayeshsorathia.blogspot.com/2012/07/net-tips-ping-ip-address-or-website.html
Comment posted by ane on Wednesday, August 22, 2012 9:46 PM
hi im doing a code for a ping function,can u able to show how to do it in a form?using c# langauage?
Comment posted by nb on Wednesday, October 3, 2012 4:53 AM
fghfgh
Comment posted by sdf on Tuesday, January 22, 2013 1:42 AM
sdf
Comment posted by Alex on Tuesday, December 3, 2013 3:08 PM
The SendAsync() example for VB.NET is wrong. It has a method as "private static void" where such does not exist in that language.
Comment posted by Boobies on Thursday, October 30, 2014 4:49 AM
Boobbzzzzz

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

FREE .NET MAGAZINES

Free DNC .NET Magazine

Tags

JQUERY COOKBOOK

jQuery CookBook