Trace Route Utility in ASP.Net C#
Introduction
Every connection to the internet has an IP address, or number, in simple terms you can say it is much like a phone number, which is unique. In this example we will use www.google.com as our site to trace route, and as you will see when you run this example that site will resolve to an IP address of 74.125.47.104.
As I mentioned earlier every connection has a unique number, BUT, remembering these numbers is difficult for most people, so these IP's also have Uniform Resource Locators, or URL's.
There are thousands of DNS or Domain Name Servers out there that look up these URLs (www.google.com is a URL) and translates them behind the scenes into the correct IP number, and you are then connected.
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
What is a Trace route?
A trace route traces hop for hop, all the jumps you take from your internet connection to the destination. The first few hops are usually through your service provider's network. The hops from there are usually through the backbone/upstream provider your service provider uses to route internet traffic.
The last few hops will be with our upstream/backbone provider through our server network and to your server. Each hop shows timing information (designated by 3 sets of 'ms' ratings). Timing below 300 ms is good timing. Anything above that up to 1000 ms indicates some delays which will ultimately affect your overall connection performance. Any 'ms' timings represented by an asterisk (*) indicate a timeout (bad connection).
Whatever hops shows asterisks or timings above 500 will, more than likely, be where your connection is having problems. If the problems occur, in the first few hops, it is your service provider. If the problems occur within the midway hops, it is your service provider's upstream/backbone connection.
If the problem occurs within the last few hops to your site, then it is a problem on the server end. Please note, if the problem occurs in the first few hops, it will affect most of the hops thereafter.
What we will learn in this article:
- What is a Trace Route
- How to create a Trace Route Utility for ASP.Net
Getting started with our example
The first thing we need to do to get started with our application is to create a new web application.
Start Visual Studio 2005 and create new Web Application project as illustrated in Fig. 1:
When you select the “OK” button the project will be created.
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
The next step in our example is to add the Trace Route code behind. For this example we are adding the code to the Page Load event of the application. When adding this to your application you might want to use another source for display rather than the web page itself, but for this example we are just trying to show you how easy it is to create your own Trace Route utility and we will utilize the web page for our output display.
Here is the code for the Page Load event:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
private System.Diagnostics.Process p;
protected void Page_Load(object sender, EventArgs e)
{
try
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Parse("6.7.1967 08:19:00"));
Page.Response.Clear();
Page.Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "filename=\"traceroute.txt\"");
Response.BufferOutput = false;
Page.Response.Output.Write("TraceRoute Information!\n\n");
Page.Response.Flush();
p = new System.Diagnostics.Process();
p.StartInfo.FileName = "c:\\windows\\system32\\tracert";
p.StartInfo.Arguments = "www.google.com";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.PriorityClass = System.Diagnostics.ProcessPriorityClass.Normal;
string strTemp = String.Empty;
while (strTemp != null && !p.HasExited)
{
strTemp = p.StandardOutput.ReadLine();
if (strTemp.Length > 3) Page.Response.Output.Write(strTemp + "\n");
Page.Response.Flush();
if (!Response.IsClientConnected)
{
p.Kill();
break;
}
}
Response.Flush();
Response.Close();
}
catch (Exception ex)
{
// Please Note:
// When you add this to your application you will want to change the
// exception handling to throw the correct exceptions rather than the
// generic Exception.
}
}
}
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
Here is what our example will look like when it is run.
Fig. 2
What we have Learned
We have learned that we can easily write a utility that will allow us to Trace Route an IP address or a URL. If we use a URL we will see that the process will resolve that URL to its correct IP address.
Attachments





