In this tip, we will create a small utility using C# which tells us whether our API server is up or down.
Our purpose is to check the API server, so that we can make a request for our resources. It is recommended that before making any request to server (API server in our case), we should make sure that our server is ready and up to handle our requests.
Throughout this tip, we will create a tiny utility to solve our purpose.
So, what do we require? We just want to know the status of our Web API server.
Let's create a simple console app, which tells us about the status of our API server:
- Open Visual Studio
- Create a C# console application, name it ‘
knowserverstatus
’

- Add the following method:
public static void ServerStatusBy(string url)
{
Ping pingSender = new Ping();
PingReply reply = pingSender.Send(url);
Console.WriteLine("Status of Host: {0}", url);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("IP 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);
}
else
Console.WriteLine(reply.Status);
}
Do not forget to add the following namespace:
using System.Net.NetworkInformation;
- Make a call from
main
:
Console.Write("Enter server url:");
var url = Console.ReadLine();
Console.Clear();
ServerStatusBy(url);
Console.ReadLine();
- Run and we will get the following output:


Let's elaborate the above code, here PING sends an ICMP (Internet Control Message Protocol) request to the requested server host/url and waits to receive the response back. If the value of Status is success, it means our server is up and running.
Here PingReply
returns all properties, described as:
Proeprty | Description |
Address | Gets the address of Host |
Buffer | Gets the buffer of data received, it's an array of bytes |
PingOption | It handles how data is transmitted |
RoundTrip | Gets the number of milliseconds to send ICMP echo request |
Status | Gets the status of request. If not Success, it means Host is not up |
In this tip, we discussed all about PingReply
class and with the help of this class, we created a utility to know the status of our server.
History
- 25th March, 2015: Initial version
Feedback
Feel free to leave any feedback on this tip; it is a pleasure to see your comments and vote about this code. If you have any questions, please do not hesitate to ask me here.