Joshnet Web Development Blue Dragon Logo Joshnet Web Development

Enter Site To Ping

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

protected void PingSiteNow_Click(object sender, EventArgs e)
{
string Target = SiteToPing.Text;
Ping PingSite = new Ping();
PingOptions Options = new PingOptions();
// Use the default Ttl value which is 128
// but change the fragmentation behavior.
Options.DontFragment = true;
// Create a buffer of 32 bytes of data to be transmitted.
string data = "12345678901234567890123456789012";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
PingReply reply = PingSite.Send(Target, timeout, buffer, Options);
if (reply.Status == IPStatus.Success)
{
//write results
Results.InnerHtml = "<div>Address: " + reply.Address.ToString() + "</div>";
Results.InnerHtml += "<div>RoundTrip time: " + reply.RoundtripTime.ToString() + "</div>";
Results.InnerHtml += "<div>Time to live: " + reply.Options.Ttl.ToString() + "</div>";
Results.InnerHtml += "<div>Don't fragment: " + reply.Options.DontFragment.ToString() + "</div>";
Results.InnerHtml += "<div>Buffer size: " + reply.Buffer.Length.ToString() + "</div>";
}
else
{
Results.InnerHtml = "Could not ping host";
}
}
This is a simple C# code used to ping a remote site from the internet. Feel free to use This code as you want on any of your ASP.NET websites.