Home ProgrammingASP.NET Ping an IP address from ASP.NET

Ping an IP address from ASP.NET

by John Bhatt
1596 views
Ping from ASP.NET

Today, I am going to share something useful. In this tutorial we will learn how to ping an IP address or a host using ASP.NET.

Ping from ASP.NET

Ping from ASP.NET - Web Page Design

Directly moving to a design. Below is the simplest possible design for Ping. We have used a textbox, a button and a textarea.

Code for ASPX Page.

 <h2>Pinging from ASP.NET</h2>
 <table border="1">
 <tr>
 <td>
 <table>

 <tr>
 <td>IP Address </td>
 <td>
 <asp:TextBox ID="txtIPAddr" runat="server" Width="200px"></asp:TextBox>
    
 <asp:Button ID="btnPing" runat="server" Text="Ping" OnClick="btnPing_Click" /></td>
 </tr>
 <tr>
 <td>
 Reply
 </td>
 <td colspan="2">
 <textarea id="txtPingReply" runat="server" rows="2" cols="80" style="background-color: black; font-family: 'Courier New'; color: white"></textarea>
 </td>
 </tr>
 </table>
 </td>
 </tr>
 </table>

Now, just add below line of code on button click event. But before that, ensure to add a namespace which is mentioned below.

using System.Net.NetworkInformation;

Now button click code to Display Ping Results

 protected void btnPing_Click(object sender, EventArgs e)
 {
 Ping p = new Ping();
 PingReply r;
 String s = txtIPAddr.Text;
 r = p.Send(s);
 Response.Buffer = false;
 if (r.Status != IPStatus.Success)
 {
 txtPingReply.InnerText = "Sorry! Can not ping due to unknown reason.";
 }
 else if (r.Status == IPStatus.Success)
 {
 txtPingReply.InnerText = "Reply from " + s.ToString() + "[" + r.Address.ToString() + "] bytes = "
 + r.Buffer.Length.ToString() + " time= " + r.RoundtripTime.ToString() + " ms." + "n";
 }
 }
Ping from ASP.NET : Webpage with ping Reply

Ok, Now lets Run page and see the outcome by putting a hostname or IP address.

We have successfully received reply using ping from asp.net. If you want to know more about Pinging in C#, you can learn a lot from below article published at CodeProject.
Thanks for reading.

If you like this article and find helpful anyway, you can help use by sharing on social networks or leaving comments with suggestions or feedback.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.