create a http server with a trace route in csharp

To create an HTTP server with trace route in C#, you can use the System.Net namespace to launch a HttpListener which will respond to HTTP requests. You can use the TraceRoute class to perform a trace route for incoming requests.

main.cs
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

class HttpServer
{
    static void Main(string[] args)
    {
        HttpListener listener = new HttpListener();
        listener.Prefixes.Add("http://localhost:8080/");
        listener.Start();

        Console.WriteLine("Listening...");

        while (true) {
            HttpListenerContext context = listener.GetContext();
            HttpListenerRequest request = context.Request;

            // Perform trace route for incoming request
            string traceRouteResult = TraceRoute(request.RemoteEndPoint.Address);

            // Generate response to send back to client
            string responseString = "<html><body><h1>Trace Route Results</h1><pre>" + traceRouteResult + "</pre></body></html>";
            byte[] buffer = Encoding.UTF8.GetBytes(responseString);
            context.Response.ContentLength64 = buffer.Length;
            context.Response.OutputStream.Write(buffer, 0, buffer.Length);
            context.Response.Close();
        }
    }

    static string TraceRoute(IPAddress ip)
    {
        StringBuilder traceRouteResult = new StringBuilder();
        Ping ping = new Ping();
        PingOptions pingOptions = new PingOptions(1, true);
        PingReply pingReply = null;

        traceRouteResult.AppendLine("Tracing route to " + ip.ToString());
        traceRouteResult.AppendLine();

        for (int ttl = 1; ttl <= 30; ttl++) {
            pingOptions.Ttl = ttl;
            traceRouteResult.Append(ttl.ToString().PadLeft(2, ' '));
            traceRouteResult.Append(" ");

            string hostName = "";
            try {
                pingReply = ping.Send(ip, 1000, new byte[32], pingOptions);
                if (pingReply.Status == IPStatus.Success) {
                    hostName = GetHostName(pingReply.Address.ToString());
                    traceRouteResult.Append(pingReply.Address.ToString().PadRight(16, ' '));
                    traceRouteResult.AppendLine(hostName.PadRight(32, ' '));
                }
                else {
                    traceRouteResult.AppendLine("* * * Request timed out");
                }
            }
            catch (PingException pe) {
                traceRouteResult.AppendLine(pe.Message + " " + hostName);
            }

            if (pingReply != null && pingReply.Status == IPStatus.Success) {
                if (pingReply.Address.Equals(ip)) {
                    traceRouteResult.AppendLine("Tracing complete.");
                    break;
                }
            }
        }

        return traceRouteResult.ToString();
    }

    static string GetHostName(string ipAddress)
    {
        try {
            IPHostEntry ipHE = Dns.GetHostEntry(ipAddress);
            if (ipHE.HostName != null && ipHE.HostName.Length > 0) {
                return ipHE.HostName;
            }
        }
        catch {
            return "";
        }

        return "";
    }
}
2974 chars
89 lines

gistlibby LogSnag