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.

(563) Comments

Blog Message https://indigo.co/Category/temporary_carpet_protection_protector_film_tape

Really good info! Also visit my web-site about Clomid pills


Blog Message http://www.montreal-escort.com/__media__/js/netsoltrademark.php?d=ms1.taes.ylc.edu.tw%2Fuserinfo.php%3Fuid%3D4058807

I think this is a real great blog article.Thanks Again. Cool.


Blog Message http://kim3124sr.biznewsselect.com/but-if-you-add-a-5th-settler-the-green-and-i-have-plenty-resources

Of course, what a great site and illuminating posts, I definitely will bookmark your site.Have an awsome day!


Blog Message http://banks.at.ua/go?http://ww88thai.com/forum/profile.php?section=personality&id=106768

Simply wanna remark that you have a very decent web site , I enjoy the design and style it actually stands out.


Blog Message http://knight-soldiers.com/2018/12/12/ciri-khas-dari-agen-bola-online-3/

Major thankies for the blog post.Really looking forward to read more. Will read on


Blog Message http://growithlarry.com/

There is visibly a lot to realize about this. I feel you made certain nice points in features also.


Blog Message http://opalclumpneruww.tubablogs.com/financial-assets-range-from-low-risk-low-return-investments-such-as-high-grade-government-bonds-to-those-1996-liberalization-was-at-snails-pace

This site was how do I say it? Relevant!! Finally I ave found something which helped me. Kudos!


Blog Message http://mel-assessment.com/members/lookgrouse73/activity/32456/

There is definately a great deal to find out about this issue. I love all of the points you ave made.


Blog Message http://sashapnl6kbt.tutorial-blog.net/unlike-most-real-estate-investments-these-are-highly-liquid-with-no-reinvest-those-dividends-to-grow-their-investment-further

Major thankies for the article.Really looking forward to read more.


Blog Message http://zelatestize.website/story.php?id=144

Really appreciate you sharing this blog post.Really looking forward to read more. Much obliged.


Blog Message http://www.elevate.design/2014/11/06/brand-management-2/

This very blog is definitely cool additionally informative. I have picked a bunch of useful tips out of this source. I ad love to visit it over and over again. Thanks!


Blog Message http://dotsecret.com/__media__/js/netsoltrademark.php?d=www.vetriera12.it%2Findex.php%3Foption%3Dcom_k2%26view%3Ditemlist%26task%3Duser%26id%3D367247

I think this is a real great blog post.Thanks Again. Awesome.


Blog Message http://diamondbytheyard.com/__media__/js/netsoltrademark.php?d=www.allcodycrossanswers.com%2Findex.php%3Fqa%3Duser%26qa_1%3Ddanielamadaras

IaаАа’б‚Т€ТšаЂаŒаАа’б‚Т€ТžаБТžll complain that you have copied materials from a different source


Blog Message https://www.newsbtc.com/2018/11/29/amazon-gets-deeper-into-blockchain-what-does-it-mean-for-crypto/

Thanks for another great post. Where else could anybody get that type of information in such an ideal way of writing? I ave a presentation next week, and I am on the look for such info.


Blog Message http://toparticlesubmissionsites.com/the-best-android-apps-2018/

Lovely website! I am loving it!! Will be back later to read some more. I am bookmarking your feeds also

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