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

Ping an IP address from ASP.NET

by John Bhatt


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

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

Code for ASPX Page.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title>Ping to IP from ASP.NET</title>
 <form id="form1" runat="server">
 <h2>Pinging from ASP.NET</h2>
 <table border="1">

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



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
 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";

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

You may also like

Leave a Reply

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

Ping an IP address from ASP.NET

by John Bhatt time to read: 2 min