How to ping IP address in asp net
In this article on how to use ping in asp.net, how can we ping a hostname/ address to check whether it is up or not?Pinging can be of help to detect the current status of a website or to check the availability of the Server.
Ping is used to testing the reachability of a host on an Internet Protocol(IP) network and to measure the round-trip time for messages sent from the originating host to a destination computer. Microsoft .Net provides Ping class which is present in System.Net.NetworkInformation Namespace to ping a machine.
Design Page using the Code below:
<body>
<form id="form1" runat="server">
<div>
<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" />--%>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</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>
</div>
</form>
</body>
using System.Net; using System.Net.NetworkInformation; using System.Text;
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";
}

Post A Comment:
0 comments: