Click here to Skip to main content
15,887,957 members
Articles / All Topics

Ping remote machines using .NET

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
10 Jan 2011CPOL 14.2K   6   1
Ping remote machines using .NET

If you are looking to ping a remote machine and you want to record the state along with status and return time, the below example can help you.

C#
private void Ping_Click(object sender, EventArgs e)
{
    List domains = new List();
    domains.Add("www.bing.com");
    domains.Add("www.yahoo.com");
    domains.Add("jebarson.info");
    
    foreach (string domain in domains)
    {
        Ping pinger = new Ping();
        UserToken token = new UserToken() 
        { Destination = domain, InitiatedTime = DateTime.Now };
        pinger.PingCompleted += new PingCompletedEventHandler(PingCompleted);
        pinger.SendAsync(domain, token);
    }
}

private void PingCompleted(object sender, PingCompletedEventArgs e)
{
    UserToken token = (UserToken)e.UserState;
    Debug.Assert(true, string.Format("Reply from 
    {0} with the status {1}", token.Destination, e.Reply.Status));
}

public class UserToken
{
    public string Destination { get; set; }
    public DateTime InitiatedTime { get; set; }
}

Always remember that the property e.Reply.Address will show up the correct destination IP address only if it is reachable. I would suggest you to go through the following MSDN URLs which will give you more insight.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Microsoft Corporation
India India
I work for Microsoft on MS technologies for application development. My interests include .net, WCF, Azure, Windows Phone, ASP.net, SL, WCF, WPF and many more.

You can visit my site at http://www.jebarson.info

Follow me on twitter @jebarson007

Comments and Discussions

 
GeneralThis is very good article. Pin
Jayesh Sorathia11-Jul-12 22:29
Jayesh Sorathia11-Jul-12 22:29 

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.