65.9K
CodeProject is changing. Read more.
Home

Ping remote machines using .NET

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Jan 10, 2011

CPOL
viewsIcon

14397

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.

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.