Ping on server with C #

1

Is there any .NET class with ping functionality? I have the IP of the server in an attribute of type String and would like to ping the server to see if it is online or offline. Does anyone know?

    
asked by anonymous 20.09.2016 / 17:40

2 answers

2

There is the Ping class, use example:

public static bool pingServidor(string nameOrAddress)
{
    bool pingable = false;
    Ping pinger = new Ping();
    PingReply reply = pinger.Send(nameOrAddress);
    return reply.Status == IPStatus.Success;
}
    
20.09.2016 / 18:36
2

There is this class Ping

public class PingExample
    {
        // args[0] can be an IPaddress or host name.
        public static void Main (string[] args)
        {
            Ping pingSender = new Ping ();
            PingOptions options = new PingOptions ();

            // Use the default Ttl value which is 128,
            // but change the fragmentation behavior.
            options.DontFragment = true;

            // Create a buffer of 32 bytes of data to be transmitted.
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes (data);
            int timeout = 120;
            PingReply reply = pingSender.Send (args[0], timeout, buffer, options);
            if (reply.Status == IPStatus.Success)
            {
                Console.WriteLine ("Address: {0}", reply.Address.ToString ());
                Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
                Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
                Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
                Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
            }
        }
    }

Updating. There is a simpler constructor for Send : Link

    
20.09.2016 / 17:45