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 a lot of others too using TCP ports. We will need to use the System.Net.Sockets namespace for this function.

using System.Net.Sockets;

ServicePing

We will create a TcpClient and try to connect the the server or service we want to check. If it connects, the server or service is online. If the connection fails the TcpClient will throw an exception. That is how we know if the server or service is offline.

The method will return a bool about the status of the server. It will also take the address and port number as parameters.

public static bool ServicePing(string address, int port)
{
    bool serviceOnline;
    
    TcpClient tcpClient = new TcpClient();
    try {
        tcpClient.Connect(address, port);
        serviceOnline = true;     // Service is online. No exception thrown.
    }
    catch(Exception) {
        serviceOnline = false;    // Exception was thrown. Service is offline
    }

    return serviceOnline;
}

If we want to check if a web server is online, we check if we can connect to port 80. We can check if google is online by doing something like this.

string address = "google.com";
int port = 80;

if (ServicePing(address, port) == true)
    Console.WriteLine("{0}:{1} is online", address, port);
else
    Console.WriteLine("{0}:{1} is offline", address, port);

Most of the time this will print out this.

google.com:80 is online

You can check if your MySQL is online. I have one at my home network called db2.home.

string address = "db2.home";  // My internal MySQL server
int port = 3306;              // default port for MySQL

if (ServicePing(address, port) == true)
    Console.WriteLine("{0}:{1} is online", address, port);
else
    Console.WriteLine("{0}:{1} is offline", address, port);

To check out my other C# stuff.

About Author

Related Posts

C# Reference Types

Understanding C# Reference Types

One of the key features of C# is its support for C# reference types, which allow developers to create complex, object-oriented applications. In this blog post, we…

c# value types

Understanding C# Value Types

C# is a powerful programming language that is widely used for developing a wide range of applications. One of the key features of C# is its support…

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…

csharp ping check

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…

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