Welcome to Ultra Developers.

the company that developes your success

Read Blog


How to: Check that a server is reachable across the network using C#

How to: Check that a server is reachable across the network using C#

Sometimes your application connects to other servers for any purpose such as querying a database, sending e-mails, sending SMS and etc. You need to make sure that the server is reachable across the network. One simple method is to ping it using the command line by writing the following command:

Ping [server IP address]

But if you want to make sure the server is reachable across the network from your application! The good news is that the System.Net.NetworkInformation has a Ping class that is used to ping network servers. In this article we will see how to use the Ping class to check the availability of a server. You can use Ping and related classes to check whether a computer is reachable across the network.

Using the Code:

To make your application make sure a server is reachable across the network, follow these steps:

  1. Create a new windows application using Visual Studio 2005/2008.
  2. Rename Form1 to PingForm.
  3. Add a TextBox to the PingForm and rename it to IPTextBox.
  4. Import the System.Net and System.Net.NetworkInformation namespaces using the following statement:
using System.Net;
using System.Net.NetworkInformation;
  1. The System.Net namespace provides a simple programming interface for many of the protocols used on networks today.
  2. The System.Net.NetworkInformation namespace provides access to network traffic data, network address information, and notification of address changes for the local computer.
  3. Add a button to the PingForm and name it PingButton and double click the PingButton in the designer to create the Click event handler in the code.
  4. Add the following code to the PingButton Click Event Handler:
private void PingButton_Click(object sender, EventArgs e)
{
    try
    {
        if (string.IsNullOrEmpty(IPTextBox.Text))
            return;

        string host = IPTextBox.Text;
        IPAddress hostAddress = Dns.GetHostEntry(host).AddressList[0];

        Ping ping = new Ping();
        PingReply pingReply = ping.Send(hostAddress);

        if (pingReply.Status == IPStatus.Success)
        {
            MessageBox.Show("Server ( " + pingReply.Address.ToString()
                + " ) is reachable.", pingReply.Status.ToString(),
                MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        else
        {
            MessageBox.Show("Server is unreachable.",pingReply.Status.ToString(),
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }
    catch (PingException ex)
    {
        MessageBox.Show(ex.Message, ex.GetType().ToString(),
            MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, ex.GetType().ToString(),
            MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
  1. In the above code:
    • We check that the IPTextBox text is not empty or null.
    • Create a string object named host that will contain the Text entered by the user in the IPTextBox.
    • Create a new object named hostAddress of type IPAddress class. This class provides an Internet Protocol (IP) address and the IPAddress class contains the address of a computer on an IP network.
    • We use the Dns class that provides simple domain name resolution functionality to get the IP address if the user types the server name by using the GetHosEntry method that resolves a host name or IP address to an IPHostEntry instance.
    • Create a new object of the Ping class. This object is used to send a ping request to the server by using the Send method.
    • The ping object Send method attempts to send an Internet Control Message Protocol (ICMP) echo message to the computer that has the specified IPAddress, and receive a corresponding ICMP echo reply message from that computer.
    • Create a PingReply object that is used to store the reply message returned from the Send method.
    • We then check the status of the pingReply object by using the Status property.
    • If the Status equals to success we display a message box that tell the user that the server is reachable across the network.
    • If any other status is returned we display a message box to tell the user that the server is not reachable across the network and tell him the status returned from the ping operation.
  2. Now compile and run the application.
  3. Enter any IP address or Name of the computer you want to ping and click on the PingButton.
  4. You will be notified if the IP address you typed is reached or not.

Now you have an application that can check whether a computer is reachable across the network.

Similar Posts