Apr 6, 2007

Network Classes in the .NET Framework

The Microsoft .NET Framework includes classes that make it practical and simple to build network applications and utilities. In this article, I’m going to walk you through the construction of a trace route utility. When you communicate with any server on the Internet, your traffic is routed through many network nodes (routers and other servers) before reaching the final destination. A trace route utility provides information about all the intermediate nodes between you and the destination IP address.

The following is the output of my trace route utility, reporting the network path from my machine to Google:

Tracing route to www.google.com local.gateway 0ms seared2-dhcp-207-115-94-97.isomedia.com 44ms core-01-ge-0-2.sttl.isomedia.com 45ms bord-04-ge-2-0.sttl.isomedia.com 335ms 45ms g12-1.core01.sea01.atlas.cogentco.com 48ms p6-0.core02.smf01.atlas.cogentco.com 63ms p4-0.core02.sfo01.atlas.cogentco.com 63ms t3-3.mpd01.sfo01.atlas.cogentco.com 62ms t7-2.mpd01.sjc01.atlas.cogentco.com 68ms v3491.mpd01.sjc03.atlas.cogentco.com 70ms g0-0-0.core01.sjc03.atlas.cogentco.com 70ms google.sjc03.atlas.cogentco.com 70ms 82ms 80ms 83ms 85ms mc-in-f147.google.com 81ms

Press return to exit

You can see that the utility reports the IP address, the host name (if available), and the ping response time to that host. If you look carefully, you can see that my traffic went from Portland to Seattle through San Francisco and finally to San Jose. In all, it took 18 hops to get to Google.

The following is the complete code for the trace route application. You can simply copy and paste this code into a new C# console application to experiment with it yourself:

using System;

using System.Collections.Generic;

using System.Text;

using System.Net;

using System.Net.NetworkInformation;

namespace ConsoleApplication2


class Program


static string destination = "www.google.com";

static void Main(string[] args)


Console.WriteLine("Tracing route to {0}", destination);

IPAddress ip = Dns.GetHostEntry(destination).AddressList[0];

Ping pinger = new Ping();

for (int ttl = 1; ttl <>


byte[] b = new byte[256];

PingReply reply = pinger.Send(

ip, 3000, b, new PingOptions(ttl, true));

if (reply.Address == null)







string hostName;



hostName = Dns.Resolve(



catch (Exception)


hostName = "";


PingReply roundTripReply = pinger.Send(reply.Address);

Console.WriteLine("{0} {1} {2}ms", reply.Address.ToString(),




if (reply.Address.ToString() == ip.ToString()) break;


Console.WriteLine("Press return to exit");





This application uses classes in the System.Net.NetworkInformation namespace. First, the application converts the host name (www.google.com in this example) to an IP address. Then, the application attempts to ping the IP address. However, ping requests can be configured with a Time to Live (TTL) parameter. Each time the packet passes through a network node the node decrements the TTL parameter. If a node decrements the TTL parameter to zero, it doesn't forward the packet and instead returns an error to the caller.

To trace the route from your location to a distant IP address, you perform multiple ping attempts. On the first attempt, the TTL parameter is set to 1, causing the first network node to cease forwarding and return an error. The error contains information about that node. On the next attempt, the TTL parameter is set to 2, causing the second node in the path to return an error. The TTL parameter is incremented by 1, causing each subsequent node to return an error until the destination is reached. This provides information about every node on the path between your machine and the destination server.