Implementing Ping in ASP.Net
In this Article We will see, how can we ping a host name/ address to check whether it is up or not..
Pinging can be of great help to detect the current status of a website or to check the availability of the Server...
First of All add the following Namespaces:
using System.Net; using System.Net.NetworkInformation; using System.Text;
Then Create the Design Page using the Code below:
Finally Use the following Code Snippet in the Code Behind:
What This Code will do for you is that It will ping the entered website in the text-box every 4 second and display the ping result in the textbox.
<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc"> <tr> <td width="100" align="right" bgcolor="#eeeeee" class="header1"> Hostname/IP: </td> <td align="center" bgcolor="#FFFFFF"> <asp:TextBox ID="txtHost" runat="server"></asp:TextBox> <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" /> </td> </tr> <tr> <td width="100" align="right" bgcolor="#eeeeee" class="header1"> Ping Results: </td> <td align="center" bgcolor="#FFFFFF"> <asp:TextBox ID="txtPing" runat="server" Height="400px" TextMode="MultiLine" Width="400px"></asp:TextBox> <asp:Label ID="lblStatus" runat="server"></asp:Label> </td> </tr> </table> <asp:Timer ID="Timer1" runat="server" Interval="4000" OnTick="Timer1_Tick"> </asp:Timer> </ContentTemplate> </asp:UpdatePanel>
Finally Use the following Code Snippet in the Code Behind:
protected void Page_Load(object sender, EventArgs e) { } protected void btnSubmit_Click(object sender, EventArgs e) { try { lblStatus.Text = ""; sendSubmit(); } catch (Exception err) { lblStatus.Text = err.Message; } } protected void Timer1_Tick(object sender, EventArgs e) { if(txtHost.Text != "") sendSubmit(); } public void sendSubmit() { Ping ping = new Ping(); PingReply pingreply = ping.Send(txtHost.Text); txtPing.Text += "\r\nAddress: " + pingreply.Address + "\r\n"; txtPing.Text += "Roundtrip Time: " + pingreply.RoundtripTime + "\r\n"; txtPing.Text += "TTL (Time To Live): " + pingreply.Options.Ttl + "\r\n"; txtPing.Text += "Buffer Size: " + pingreply.Buffer.Length.ToString() + "\r\n"; }
What This Code will do for you is that It will ping the entered website in the text-box every 4 second and display the ping result in the textbox.
No comments:
Post a Comment
Thank You for Your Comments. We will get back to you soon.