Search code examples
c#.netwinformstraceroute

Traceroute Results


I’ve been looking at Traceroute code from a blog post.

The problem is that when I try to get the results to C# Form. The only thing I get is the completed IP on one row; hop, ms and IP. I was trying to get a list or be able to add (string (Tracing route to 172.217.7.174 over a maximum of 30 hops:) hop, ms and IP Address) to a ListView.

private async void btnAwait_Click(object sender, EventArgs e)
{
    var Results = await asyncTraceRoute.TryTraceRouteInternalAsync("google.com", 30);
    txtTraceroute.Text = Results.Message;
}

Help on this would be greatly appreciated.


Solution

  • The name of the method (TryTraceRouteInternalAsync) actually gives a hint that something is being done in an incorrect manner.

    This method invokes only one ping and that's why you get just one result.

    The actual method you need to invoke is TryTraceRouteAsync but I have a strong feeling that you had chosen the other one because it returns an accessible object, with a string Message property.

    If you do the following, the TryTraceRouteAsync method will asynchronously invoke TryTraceRouteInternalAsync (MAX_HOPS = 15) times simultaneously and write all the results into the streamWriter.

    You can just call this method and get all the results you are looking for:

        private async Task<string> TraceRt()
        {
            MemoryStream memoryStream = new MemoryStream();
            StreamWriter streamWriter = new StreamWriter(memoryStream);
            await TraceRoute.TryTraceRouteAsync("google.com.tr", streamWriter);
            streamWriter.Flush();
            memoryStream.Position = 0;
            StreamReader stringReader = new StreamReader(memoryStream);
            return stringReader.ReadToEnd();
        }