News
  • Favorite
  • Facebook
  • Twitter
  • LinkedIn

Microsoft Press Pearson Education (InformIT)
Skip Navigation Links
Pearson Education (InformIT)
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;

5.       The System.Net namespace provides a simple programming interface for many of the protocols used on networks today.

6.       The System.Net.NetworkInformation namespace provides access to network traffic data, network address information, and notification of address changes for the local computer.

7.       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.

8.       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);

    }

}

a.       In the above code we check that the IPTextBox text is not empty or null.

b.      Create a string object named host that will contain the Text entered by the user in the IPTextBox.

c.       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.

d.      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.

e.      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.

f.        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.

g.       Create a PingReply object that is used to store the reply message returned from the Send method.

h.      We then check the status of the pingReply object by using the Status property.

i.         If the Status equals to success we display a message box that tell the user that the server is reachable across the network.

j.        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.

9.       Now compile and run the application.

10.   Enter any IP address or Name of the computer you want to ping and click on the PingButton.

11.   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.

(600) Comments

Blog Message http://casathome.ihep.ac.cn/team_display.php?teamid=97234

whoah this weblog is great i really like studying your articles. Stay up the great work! You already know, lots of persons are looking round for this information, you can aid them greatly.


Blog Message https://vimeo.com/TamiaHeberts

Wow, this paragraph is fastidious, my sister is analyzing such things, thus I am going to convey her.


Blog Message https://visual.ly/users/AlisonHansen/account

This can be a set of words, not an essay. you might be incompetent


Blog Message http://b3.zcubes.com/v.aspx?mid=1361633

There is evidently a bundle to identify about this. I suppose you made certain good points in features also.


Blog Message https://www.anobii.com/groups/015f338349f04d1b56

Wow! Thank you! I continually wanted to write on my blog something like that. Can I take a portion of your post to my website?


Blog Message https://www.burdastyle.com/profiles/hathis

Magnificent web site. A lot of helpful information here. I am sending it to several pals ans also sharing in delicious. And obviously, thanks for your sweat!


Blog Message https://www.fiverr.com/glaeyr/analyser-votre-diaporama-powerpoint-et-vous-conseiller

This particular blog is no doubt entertaining and besides informative. I have picked a bunch of useful things out of this blog. I ad love to visit it again and again. Thanks a bunch!


Blog Message https://orcid.org/0000-0002-4336-6577

Rattling clean internet internet site , appreciate it for this post.


Blog Message https://foursquare.com/user/557059198

Your style is really unique compared to other people I have read stuff from. Many thanks for posting when you have the opportunity, Guess I will just bookmark this page.


Blog Message https://www.scarymazegame367.net

pretty valuable stuff, overall I imagine this is really worth a bookmark, thanks


Blog Message http://www.seoinvancouver.com/index.php/78-seo-vancouver/586-seo-surrey-bc-canada

Really good post! Also visit my web blog about Clomiphene Citrate side effects


Blog Message http://b3.zcubes.com/v.aspx?mid=

Thank you for your article.Really looking forward to read more. Want more.


Blog Message https://www.ramniwasadvt.in/contact/

Really appreciate you sharing this article post.Really thank you! Awesome.


Blog Message https://www.kouponkabla.com/michaels-coupon-framing-2019-latest-here

Utterly indited written content , regards for information.


Blog Message https://www.kouponkabla.com/dr-colorchip-coupon-2019-get-new-here

Wow! Thank you! I permanently wanted to write on my site something like that. Can I include a part of your post to my blog?

First Previous 1 2 3 4 5 6 7 8 9 10  ... Next Last 
Rating
Name
Email
Comment