SIGN UP MEMBER LOGIN:    
ARTICLE

Ping Tool in C#

Posted by Sam Hobbs Articles | Windows Forms C# March 22, 2010
This article shows how to use the Ping and PingReply classes in a C# Forms application.
Reader Level:
Download Files:
 

This article shows how to use the Ping and PingReply classes in a C# Forms application.

Unlike most ping utilities, this one is intended to notify us when a ping is successful, but this sample can easily be modified to notify us when a ping fails. This sample also shows use of a timer control in a form and of toggling a ToolStripMenuItem.

Most ping utilities are intended to notify us when something has stopped working. I wrote this utility because I needed something to notify me when my host has started working. This sample can certainly be used in a utility that does the reverse; that is, does something when a host stops responding.

In the sample program, there is a TextBox for a host and a TextBox for an interval of time in seconds. There are three output TextBoxes; one for the time of the last ping, one for the status of the ping and a third for an error message. The one for the time of the last ping is more for diagnostic purposes and is not really useful otherwise.

1.gif

Ping and PingReply Classes

The Ping class can be very easy to use. The following is a simplified version of what the sample does.

const int Timeout = 120;
String Data = "[012345678901234567890123456789]";
byte[] Buffer = Encoding.ASCII.GetBytes(Data);
Ping Sender = new Ping();
PingReply Reply = Sender.Send(Host, Timeout, Buffer);

The PingReply.Status field will be either IPStatus.Success or one of many values indicating a problem. The PingReply.RoundtripTime Property is another useful item to show.

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks

Posted by nonu singh May 08, 2012

Many thanks for this tool. Its work like charm :)

Posted by L K Jul 20, 2010
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor