The sample represents simple ICMP client for C# .NET with Ping functionality.
 
The code utilizes the Clever Internet Suite library.
 
 
private void btnPing_Click(object sender, EventArgs e)
{
  using (var client = new IcmpConnection())
  {
    client.NetworkStream = new NetworkStream();
    client.TimeOut = 1000;
    client.Open(HostResolver.GetIPAddress(txtHost.Text));

    txtResult.Text = "";

    for (int i = 0; i < 4; i++)
    {
      try
      {
        client.SendEchoRequest();
        var response = client.ReceiveResponse();
        if (!(response.IcmpPacket is IcmpEchoPacket))
        {
          throw new Exception("Invalid ICMP reply");
        }

        txtResult.Text += String.Format(
          "Reply from {0}: seq={1} received bytes={2} time={3}ms TTL={4}\r\n",
          txtHost.Text,
          (response.IcmpPacket as IcmpEchoPacket).SequenceNumber,
          (response.IcmpPacket as IcmpEchoPacket).Data.Length,
          response.RoundTripTime,
          response.IpHeader.Ttl);

          System.Threading.Thread.Sleep(1000);
      }
      catch (Exception ex)
      {
        txtResult.Text += " Error: " + ex.Message + "\r\n";
      }
    }
  }
}
 
public class IcmpConnection : UdpConnection
{
  private int startTicks;

  private void NextSequenceNumber()...
  private ushort CalculateChecksum(byte[] data, int index, int size)...

  public IcmpConnection()...

  public void Open(IPAddress ip)...
  public void SendRequest(IcmpPacket request)...

  public IcmpResponse ReceiveResponse()...

  public void SendEchoRequest()...

  public int Ttl { get; set; }
  public ushort Identifier { get; set; }
  public ushort SequenceNumber { get; set; }	
}
 
public void SendRequest(IcmpPacket request)
{
  SetTtl();

  using (var stream = new MemoryStream())
  {
    var buf = new byte[1024];
    int ind = 0;

    request.Build(buf, ref ind);

    if (request.Checksum == 0)
    {
      ushort checksum = CalculateChecksum(buf, 0, ind);

      int checksumInd = 2;
      Utils.ByteArrayWriteWord(checksum, buf, ref checksumInd);
    }
    stream.Write(buf, 0, ind);
    stream.Position = 0;

    startTicks = System.Environment.TickCount;
    WriteData(stream);
  }
}
 
public void SendEchoRequest()
{
  try
  {
    var echo = new IcmpEchoPacket();

    echo.Identifier = Identifier;
    echo.SequenceNumber = SequenceNumber;
    echo.Data = "abcdefghijklmnopqrstuvwabcdefghi";

    SendRequest(echo);
  }
  finally
  {
    NextSequenceNumber();
  }
}
 
public IcmpResponse ReceiveResponse()
{
  using (var stream = new MemoryStream())
  {
    ReadData(stream);
    stream.Position = 0;

    int ticks = System.Environment.TickCount - startTicks;

    int ind = (int)(stream.Length);
    var buf = new byte[ind];
    stream.Read(buf, 0, ind);
    ind = 0;

    IcmpResponse result = new IcmpResponse();
	
    result.Parse(buf, ref ind);
    result.RoundTripTime = ticks;
    return result;
  }
}
 
Have questions?
Join us Facebook   YouTube   Twitter   Telegram   Newsletter
 
Kind regards
Clever Components team
www.CleverComponents.com

Add Feedback