How to PING using C#

I recently needed to perform a check to see if a server was available before i could perform any additional operation. I decided that i would perform a simple PING check to see if the server could be reached. The code below shows what i came up with in the end.

Hope you fine it useful.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Timers;
using System.Threading;

namespace MyNamespace {
    public delegate void ConnectionStateHandler(object sender, ConnectionEventArgs e);
    public class HeartBeat {
        public event ConnectionStateHandler OnConnectionChange;
        private System.Timers.Timer pingTimer = new System.Timers.Timer() { Interval = 5000 };

        public HeartBeat() {
            AutoResetEvent waiter = new AutoResetEvent(false);
            PingOptions options = new PingOptions(64, true);
            options.DontFragment = true;

            #region Basic Data Packet to send
            string dataPacket = @"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            #endregion
            byte[] buffer = Encoding.ASCII.GetBytes(dataPacket);
            int timeout = 120;

            string hostName = "someservername";
            IPAddress address = Dns.GetHostAddresses(hostName)[0];
            Ping pingServer = new Ping();

            pingServer.PingCompleted += (pingSource, P) => {
                if (P.Reply.Status == IPStatus.Success) {
                    OnConnectionChange(this, new ConnectionEventArgs("Source", "Machine", true));
                } else {
                    OnConnectionChange(this, new ConnectionEventArgs("Source", "Machine", false));
                    pingTimer.Stop();
                }
            };

            pingTimer.Elapsed += (sender, ea) => {
                pingServer.SendAsync(address, timeout, buffer, options, waiter);
                waiter.WaitOne();
            };
            pingTimer.Start();
        }

        ~HeartBeat() {
            pingTimer.Stop();
        }

    }

    public class ConnectionEventArgs : EventArgs {
        public string Source { get; set; }
        public bool isConnected { get; set; }

        public string Target { get; set; }
        public ConnectionEventArgs(string From, string target, bool connected) {
            this.Source = From;
            this.isConnected = connected;
            this.Target = target;
        }
    }

}

To consume the class

            HeartBeat connected = new HeartBeat();
            connected.OnConnectionChange += (o, ea) => {
                if (!ea.isConnected) {
                    MessageBox.Show(string.Format("Connection Failure: Could not connect to : {0}",ea.Target));
                }
            };

its fairly simple stuff.

1 Comment

  1. Four to five associated with this liquid has to be consumed per week to obtain the desired result. Thus, in the event the muscles inside penile area are relaxed, more blood will go into the penis and an erection will occur.

Comments are closed.