C# Ping Check the easy way

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);
        }
    }
}

Related Posts

C# check if server is online

C# check if server is online directly from your code. Check servers or services like web servers, database servers like MySQL and MongoDB. You can probably check…

C# Convert Int to Char

C# convert int to char google search is giving some result which I think is not directly what some people want. Most results are giving instructions on…

c# bash script

C# Bash Script Made Easy

There are many reasons why it could be handy to run a bash script from a C# application. In 2014, before I changed to a Mac as…

go hello world

Golang Hello World, Get a Easy Fantastic Start

Golang hello world example tutorial. I assume you are new to the golang language since you found this website. Go is one of the latest programming languages…

Unity persistant datapath

Unity Persistent Data Path

The Unity persistentDataPath is read only and will return the path where you can save data that will be persitent betwen sessions. Even app updates doesn’t touch…

C# return index of a object

C# return index of a object in a List

How to return the index of a list type that is an object in C#. Most examples only shows simple string types when they show how to…

Leave a Reply