How to Ping network IP or Hostname in Silverlight Application?

Kunal2383
Posted by in Silverlight category on for Beginner level | Views : 9979 red flag
Rating: 4 out of 5  
 1 vote(s)

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.


 Download source code for How to Ping network IP or Hostname in Silverlight Application?

Introduction

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.


Code Implementation

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

image

Now this will create a 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 it’s “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 returning true or false.

    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:

    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.

Sample Application Download

Get the solution from my share: "Ping Demo Solution"



Page copy protected against web site content infringement by Copyscape

About the Author

Kunal2383
Full Name: Kunal Chowdhury
Member Level:
Member Status: Member
Member Since: 3/1/2010 12:38:55 PM
Country: India
Thanks & Regards, Kunal Chowdhury | http://www.kunal-chowdhury.com | http://twitter.com/kunal2383
http://www.kunal-chowdhury.com
He is currently working as a Silverlight application developer. Has a very good skill over C#, XAML, Silverlight & WPF. He has a good working experience in Windows 7 application (including Multitouch) development. During his professional career he worked in various technologies & delivered quality output. He never hesitates to take up challenges & work on the latest technologies in Microsoft platform. He attended various software development competition & achieved different awards. He is presently focusing on the RIA (Silverlight & WPF) & willing to become a Technology Specialist in Microsoft platform. Learning newer things, Blog posting & helping others in forums is one of his regular activity. Specialties: Silverlight Application Development, WPF Application Development, Windows 7 Application Development

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)