Click here to Skip to main content
15,881,172 members
Articles / All Topics

How to Ping Network IP or Hostname in Silverlight Application?

Rate me:
Please Sign up or sign in to vote.
4.94/5 (4 votes)
28 Sep 2011CPOL2 min read 12.6K   1  
How to ping network IP or hostname in Silverlight application?

Pinging a network IP or Hostname is not available in Silverlight. But you can do this using WCF service. In this post, I am going to implement the same thing for you. I am using Silverlight 4 here. But this can also be possible in Silverlight 3.

Create a Silverlight application with Silverlight hosting website as “ASP.NET Web Site”:

image

Now this will create an XAML page for you by default. Add one TextBox & one Button into it. We will use TextBox to enter IP Address or the Hostname & on click of the Button, it will ping that entered IP or Hostname. As a limitation to the Silverlight, you can’t ping directly from the client application. You need to create a WCF service & using that, you can easily ping. Remember there are some limitations here too as you are pinging it from the WCF hosting server.

Let’s implement our WCF service. Create a service method named PingNetwork and pass the hostNameOrAddress as a string. This will be your IP address or the host’s DNS name. Then create an instance of System.Net.NetworkInformation.Ping & pass the required parameter to its “Send” method. This will return you “PingReply”. Now check the Status of the reply. There are several options available. I used only IPStatus.Success to check it and depending upon that, return true or false.

C#
public bool PingNetwork(string hostNameOrAddress)
{
    bool pingStatus = false;

    using (Ping p = new Ping())
    {
        string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        byte[] buffer = Encoding.ASCII.GetBytes(data);
        int timeout = 120;

        try
        {
            PingReply reply = p.Send(hostNameOrAddress, timeout, buffer);
            pingStatus = (reply.Status == IPStatus.Success);
        }
        catch (Exception)
        {
            pingStatus = false;
        }
    }

    return pingStatus;
}

Now come to the client side implementation. Add the service reference to the Silverlight application and then call the service method with your IP Address or the DNS name of the host:

C#
client.PingNetworkAsync("google.com");

As it is an Asynchronous call, implement the “Completed” event for the method. In the completed event, check the e.Result value. If the server is able to ping, it will return true & in other case it will return false.

This is a simple implementation of the logic. As told earlier, this will ping from server & not from the client.

This article was originally posted at http://kunal2383.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
-- There are no messages in this forum --