Friday 17 January 2014

Pinging in ASP.NET Example

Leave a Comment
This tutorial will show you how to ping a hostname/ip using the .NET System.Net class, ASP.NET 2.0 and C#.NET. The .NET Framework offers a number of types that makes accessing resources on the network easy to use. To perform a simple ping, we will need to use the System.Net, System.Net.Network.Information, System.Text namespaces.

using System.Net;
using System.Net.NetworkInformation;
using System.Text;

ASPX Page

 <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>
                        &nbsp;&nbsp;
                        <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>&nbsp;<br />
                        <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>


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

Output Example




By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.