Jebarson's dev blog

Coffee, Code

Ping remote machines using .net

If you are looking for to ping a remote machine and you want to record the state along with status and return time, 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 msdn urls http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.pingcompleted.aspx and http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.sendasync.aspx which will give you more insight.

Ping remote machines using .net

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top