August 2, 2021

soltveit.org

..Geekish..

C# Ping Check the easy way

csharp ping check

How can we check if a computer, server, or another device is online? We can use a C# Ping check. If you can check the device with the ping command from the terminal or another ping tool, you can easily do the same with C#.

The easiest and most reliable method is to use the Ping class. You can also use the HttpClient. HttpClient is not reliable if you ping a password-protected area or some sort of API.

C# Ping check example

We need two lines of code to do a ping check. Let us have a look. It is using the System.Net.NetworkInformation namespace.

using System.Net.NetworkInformation;

// Create the ping object
Ping ping = new Ping();

// Create ping reply and send the ping
PingReply pingReply = ping.Send("1.1.1.1");

That is all you need to do a C# ping check. Now, it would be nice to use the results. And to see if the server was found. In this case we are pinging the CloudFlare DNS. The ping reply contain several properties we can use. The most important one for us now is if the 1.1.1.1 is up or not.

// Write out the status
WriteLine(pingReply.Status);

The pingReply.Status is an enum containing a lot of possible responces we can get. Some example responces:

Success     0
TimedOut    11010

Success obviously means the address we pinged replied. TimedOut means we did not get a reply within the default timeout period of 5 seconds. You can use a longer timeout period by adding it as a parameter after the address. Like this:

PingReply pingReply = ping.Send("1.1.1.1", 15000);  // 15 seconds, or 15000 miliseconds

Make it a Ping function

Unless you only need to use it one time, it can be a good idea to make it into a function. This is the synchronized version. Meaning it will run and wait until it’s done before running any other code. If you are checking a bunch of sites it would be better to use asynchronized function.

static bool PingCheck(string address)
{
    Ping ping = new Ping();
    PingReply pingReply = ping.Send(address, 1000);
    return pingReply.Status == IPStatus.Success;
}

This function is a bool function, so it will return true if the address replied within 1000 miliseconds. And false if it did not reply within the timeout period.

The output from the function will be different compared to just using the pingReply.Status. This function we made will return true or false because we are checking if pingReply.Status equals Success. If it does it returns true, if it dont it returns false. But the pingReply.Status will either contain (or in most casses) Success or TimedOut.

Complete example

A complete example with the PingCheck function.

using System;
using System.Net.NetworkInformation;

namespace PingTest
{
    class Program
    {
        static bool PingCheck(string address)
        {
            Ping ping = new Ping();
            PingReply pingReply = ping.Send(address, 1000);
            return pingReply.Status == IPStatus.Success;
        }
        static void Main(string[] args)
        {
            bool pingTest = PingCheck("1.1.1.1");
            System.Console.WriteLine(pingTest);
        }
    }
}