views:

5512

answers:

7

Does anyone have C# code handy for doing a ping and traceroute to a target computer? I am looking for a pure code solution, not what I'm doing now, which is invoking the ping.exe and tracert.exe program and parsing the output. I would like something more robust.

A: 

I would start looking for them in the System.Net namespace

Pablo Marambio
+5  A: 

For the ping part, take a look at the Ping class on MSDN.

Bruno Gomes
+1  A: 

Have a look at -

http://www.codeproject.com/KB/IP/Tracert-ping.aspx

If you can afford to pay, the Dart networking widgets are pretty good:

http://www.dart.com/ctl_tracenet.aspx

There's another freebie implementation here:

http://www.digigrupp.com/ping/ and here http://www.mltek.co.uk/traceclass.aspx

Kev
+6  A: 

Although the Base Class Library includes Ping, the BCL does not include any tracert functionality.

However, a quick search reveals two open-source attempts, the first in C# the second in C++:

Portman
A: 

This one solves the ping problem without using third party

http://www.aspnettutorials.com/tutorials/network/net-ping-aspnet2-csharp.aspx

Hemanshu Bhojak
+2  A: 

Here is how to implemente traceoute http://coding.infoconex.com/post/2009/01/C-Traceroute-using-net-framework.aspx

and another how to implement Ping. http://coding.infoconex.com/post/2009/01/C-Ping-and-Traceroute.aspx

The above replicates the same behaviour as windows ping and traceroute and is very simple to implement.

+1  A: 

iven that I had to write a TraceRoute class today I figured I might as well share the source code.

using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Text;
using System.Net;

namespace Answer
{  
  public class TraceRoute
  {
    private const string Data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

    public static IEnumerable<IPAddress> GetTraceRoute(string hostNameOrAddress)
    {
      return GetTraceRoute(hostNameOrAddress, 1);
    }
    private static IEnumerable<IPAddress> GetTraceRoute(string hostNameOrAddress, int ttl)
    {
      Ping pinger = new Ping();
      PingOptions pingerOptions = new PingOptions(ttl, true);
      int timeout = 10000;
      byte[] buffer = Encoding.ASCII.GetBytes(Data);
      PingReply reply = default(PingReply);

      reply = pinger.Send(hostNameOrAddress, timeout, buffer, pingerOptions);

      List<IPAddress> result = new List<IPAddress>();
      if (reply.Status == IPStatus.Success)
      {
        result.Add(reply.Address);
      }
      else if (reply.Status == IPStatus.TtlExpired)
      {
        //add the currently returned address
        result.Add(reply.Address);
        //recurse to get the next address...
        IEnumerable<IPAddress> tempResult = default(IEnumerable<IPAddress>);
        tempResult = GetTraceRoute(hostNameOrAddress, ttl + 1);
        result.AddRange(tempResult);
      }
      else
      {
        //failure 
      }

      return result;
    }
  }
}

And a VB version for anyone that wants/needs it

Public Class TraceRoute Private Const Data As String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

Public Shared Function GetTraceRoute(ByVal hostNameOrAddress As String) As IEnumerable(Of IPAddress)
    Return GetTraceRoute(hostNameOrAddress, 1)
End Function
Private Shared Function GetTraceRoute(ByVal hostNameOrAddress As String, ByVal ttl As Integer) As IEnumerable(Of IPAddress)
    Dim pinger As Ping = New Ping
    Dim pingerOptions As PingOptions = New PingOptions(ttl, True)
    Dim timeout As Integer = 10000
    Dim buffer() As Byte = Encoding.ASCII.GetBytes(Data)
    Dim reply As PingReply

    reply = pinger.Send(hostNameOrAddress, timeout, buffer, pingerOptions)

    Dim result As List(Of IPAddress) = New List(Of IPAddress)
    If reply.Status = IPStatus.Success Then
        result.Add(reply.Address)
    ElseIf reply.Status = IPStatus.TtlExpired Then
        'add the currently returned address
        result.Add(reply.Address)
        'recurse to get the next address...
        Dim tempResult As IEnumerable(Of IPAddress)
        tempResult = GetTraceRoute(hostNameOrAddress, ttl + 1)
        result.AddRange(tempResult)
    Else
        'failure 
    End If

    Return result
End Function

End Class

Scott