Click here to Skip to main content
12,692,288 members (31,713 online)
Click here to Skip to main content
Add your own
alternative version


81 bookmarked

C# Ping Component

, 30 Mar 2005
Rate this:
Please Sign up or sign in to vote.
An easy to use C# ping component.

Sample Image - CSharpPing.jpg


While working on a project that needs to check the quality of the network connection, I've found that there is no easy way to ping another computer. I expected there would be something in the framework (somewhere in the System.NET namespace) to do this as it seems like a common thing to want to do, but I could find nothing so my search turned to the web. While I was able to find some c# ping examples, none of them were very well designed. I wanted a ping utility that would let me ping a remote machine any number of times and return the ping statistics in a sensible manner (that is, not something I'd have to parse) so I decided that I'd make my own.

Most of the ping code I saw on the net seemed to be derived from this MSDN article written by Lance Olson which was written way back when for the Beta 1 release of .Net (whether it cited him or not...). I based my ping utility on this code along with this article, written by Peter A. Bromberg, PhD, which updated the Olson's code to work with .NET version 1. Using this code as a starting point I've tried to make it a little more OO by creating a couple more classes and moving the code around, so that they do their own work. I think the code makes a lot more sense now and is much easier to understand and use. I used the Windows command line ping command as a baseline for the information I wanted to return, so I created a PingResponse object that encapsulates all and gets returned instead of an int or string. I'm not going to write up a detailed description of exactly how the code works as it is either self-explanatory or simply works because that's how you have to do it (most of the ICMP class). I've included a very simple client form to show how to use the ping utility asynchronously, which I imagine, how you'd want to use it most of the time.

Using the code

To use this component simply create a Ping object and call the PingHost or BeginPingHost function.

Synchronous Example:

private void Ping(string hostname)
    //Create ping object
    Ping netMon = new Ping();

    //Ping host (this will block until complete)
    PingResponse response = netMon.PingHost(hostname, 4);

    //Process ping response
    if (response != null)

Asynchronous Example:

//Create ping object
Ping netMon = new netMon();

private void Load()
    //Wire events (in constructor or InitializeComponent)
    netMon.PingError += new PingErrorEventHandler(netMon_PingError);
    netMon.PingStarted += new PingStartedEventHandler(netMon_PingStarted);
    netMon.PingResponse += new PingResponseEventHandler(netMon_PingResponse);
    netMon.PingCompleted += new PingCompletedEventHandler(netMon_PingCompleted);

private void Ping(string hostname)
    //Start ping
    IAsyncResult result = netMon.BeginPingHost(
                     new AsyncCallback(EndPing), hostname, 4);

private void EndPing(IAsyncResult result)

private void netMon_PingStarted(object sender, PingStartedEventArgs e)
    //Process ping started

private void netMon_PingResponse(object sender, PingResponseEventArgs e)
    //Process ping response

private void netMon_PingCompleted(object sender, PingCompletedEventArgs e)
    //Process ping completed

private void netMon_PingError(object sender, PingErrorEventArgs e)
    //Process ping error

Known Issues / Further Enhancements

  • The first ping is sometimes longer than it should be. I suspect this is because of some extra time taken to create the initial socket connection and could be eliminated by simply dropping that first ping time.
  • Currently, you can only cancel when the PingResponse event is fired. It would be nice to be able to cancel at any time and have it processed immediately.
  • I should really keep track of the ping response type per response instead of for the whole set of responses. I simply haven't got around to change the PingReponse yet.


  • Version 1.0: Initial release.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Wesley Brown
Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

Questionabout app dead Problem Pin
kernzhang823-Dec-12 21:57
memberkernzhang823-Dec-12 21:57 
QuestionPing Error Pin
manjeet ghalyan6-Jan-11 23:59
membermanjeet ghalyan6-Jan-11 23:59 
AnswerRe: Ping Error Pin
Wesley Brown11-Feb-11 9:36
memberWesley Brown11-Feb-11 9:36 
As mentioned in other messages, as of Windows XP SP2, sockets require administrator account privileges to work properly.

I would also recommend using the Ping type included with .Net 2.0 and above rather than this type (unless, of course, you must use .Net 1.1).

GeneralThanks for great code but you have some bugs. Pin
mrali8626-Sep-09 3:55
membermrali8626-Sep-09 3:55 
GeneralRe: Thanks for great code but you have some bugs. Pin
Wesley Brown1-Apr-10 11:36
memberWesley Brown1-Apr-10 11:36 
GeneralRe: Thanks for great code but you have some bugs. Pin
DizZ27-May-10 3:52
memberDizZ27-May-10 3:52 
GeneralSource code not running Pin
saurabh931-Aug-09 21:28
membersaurabh931-Aug-09 21:28 
GeneralRe: Source code not running Pin
Christian Graus8-Sep-09 21:13
mvpChristian Graus8-Sep-09 21:13 
AnswerRe: Source code not running Pin
teixeirafms10-Dec-09 1:32
memberteixeirafms10-Dec-09 1:32 
GeneralSuggestion Pin
Jared Mcguire24-Apr-08 9:54
memberJared Mcguire24-Apr-08 9:54 
GeneralRe: Suggestion Pin
CBlackburn3-Aug-08 1:07
memberCBlackburn3-Aug-08 1:07 
GeneralRe: Suggestion Pin
Arash Javadi13-May-09 22:08
memberArash Javadi13-May-09 22:08 
GeneralAnswer: Timeouts Pin
imbored26-Mar-08 11:48
memberimbored26-Mar-08 11:48 
GeneralRe: Answer: Timeouts Pin
CBlackburn3-Aug-08 1:07
memberCBlackburn3-Aug-08 1:07 
QuestionMultithread error. False success messages Pin
Gokcer Gokdal26-Feb-08 10:07
memberGokcer Gokdal26-Feb-08 10:07 
AnswerRe: Multithread error. False success messages Pin
yrleu25-Nov-08 0:14
memberyrleu25-Nov-08 0:14 
Generala lot of time... Pin
Tal Hakemon7-Mar-07 3:43
memberTal Hakemon7-Mar-07 3:43 
GeneralProblem running ur code in web application Pin
jk859-Jun-06 22:01
memberjk859-Jun-06 22:01 
GeneralRe: Problem running ur code in web application Pin
icebob7-Jan-07 23:50
membericebob7-Jan-07 23:50 
QuestionSocket Exception Pin
Ryan Homer11-Apr-06 6:38
memberRyan Homer11-Apr-06 6:38 
AnswerRe: Socket Exception Pin
Ryan Homer11-Apr-06 6:41
memberRyan Homer11-Apr-06 6:41 
GeneralRe: Socket Exception Pin
Wesley Brown11-Apr-06 8:00
memberWesley Brown11-Apr-06 8:00 
NewsRe: Socket Exception Pin
Ryan Homer12-Apr-06 3:58
memberRyan Homer12-Apr-06 3:58 
GeneralSynchronous timeout parameter not what it seems Pin
Roland_W6-Feb-06 3:41
memberRoland_W6-Feb-06 3:41 
GeneralCode update Pin
Johan Roos27-Jan-06 2:02
memberJohan Roos27-Jan-06 2:02 
GeneralRe: Code update Pin
Member 36263025-Sep-13 0:02
memberMember 36263025-Sep-13 0:02 
QuestionStatistical error? Pin
Ricardo Mendes20-Dec-05 7:19
memberRicardo Mendes20-Dec-05 7:19 
Questionerror? Pin
missjasper12-Dec-05 8:21
membermissjasper12-Dec-05 8:21 
AnswerRe: error? Pin
simonnews29-Dec-08 12:59
membersimonnews29-Dec-08 12:59 
GeneralSystem.Net.NetworkInformation.Ping Pin
Kristofer Liljeblad18-Nov-05 12:28
memberKristofer Liljeblad18-Nov-05 12:28 
GeneralRe: System.Net.NetworkInformation.Ping Pin
DigiOz Multimedia17-Oct-07 8:38
memberDigiOz Multimedia17-Oct-07 8:38 
Generalunmanaged code Pin
ThPloeger27-Sep-05 2:52
memberThPloeger27-Sep-05 2:52 
QuestionWhy 60 bytes? Pin
Einar Kvandahl29-Jul-05 12:43
memberEinar Kvandahl29-Jul-05 12:43 
GeneralAsync ping doesn't seem to return a PingResponse Pin
stevehiner1-Jul-05 13:08
memberstevehiner1-Jul-05 13:08 
GeneralRe: Async ping doesn't seem to return a PingResponse Pin
Wesley Brown12-Jul-05 9:17
memberWesley Brown12-Jul-05 9:17 
GeneralThanks! Pin
Ben Buckland22-Jun-05 0:36
memberBen Buckland22-Jun-05 0:36 
GeneralResponse from nowhere... Pin
ibleif19-Apr-05 8:26
memberibleif19-Apr-05 8:26 
GeneralRe: Response from nowhere... Pin
Tust26-Apr-05 12:09
memberTust26-Apr-05 12:09 
GeneralQuick and Dirty Ping Pin
_TAD_7-Apr-05 10:44
member_TAD_7-Apr-05 10:44 
GeneralRe: Quick and Dirty Ping Pin
milesha11-Apr-05 23:50
membermilesha11-Apr-05 23:50 
GeneralRe: Quick and Dirty Ping Pin
Wesley Brown12-Jul-05 4:50
sussWesley Brown12-Jul-05 4:50 
GeneralRe: Quick and Dirty Ping Pin
Andy@16-Sep-05 0:00
memberAndy@16-Sep-05 0:00 
GeneralFirst ping issue Pin
leppie30-Mar-05 20:31
memberleppie30-Mar-05 20:31 
GeneralRe: First ping issue Pin
Wesley Brown31-Mar-05 4:47
memberWesley Brown31-Mar-05 4:47 
GeneralRe: First ping issue Pin
Alastair Revell7-Apr-05 4:30
memberAlastair Revell7-Apr-05 4:30 
GeneralBuggy, never tested... Pin
anonymous30-Mar-05 13:53
sussanonymous30-Mar-05 13:53 
GeneralRe: Buggy, never tested... Pin
Wesley Brown31-Mar-05 5:00
sussWesley Brown31-Mar-05 5:00 
GeneralRe: Buggy, never tested... Pin
mqbfc1-Apr-05 13:29
membermqbfc1-Apr-05 13:29 
GeneralRe: Buggy, never tested... Pin
conrad10-Apr-05 7:31
memberconrad10-Apr-05 7:31 
GeneralRe: Buggy, never tested... Pin
Ben Buckland21-Jun-05 19:21
memberBen Buckland21-Jun-05 19:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 30 Mar 2005
Article Copyright 2005 by Wesley Brown
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid