Ping.Send fails while ping in dos succeeds RRS feed

  • Question

  • I'm using the synchronous ping example at MSDN's ping class.  However when I use the program to ping my gateway it fails, while ping in a normal dos prompt fails:

    From executable compiled from example:


    From DOS prompt:

    Pinging with 32 bytes of data:
    Reply from bytes=32 time=5ms TTL=64
    Reply from bytes=32 time=1ms TTL=64
    Reply from bytes=32 time=1ms TTL=64

    Furthermore, using wireshark I see that no ICMP echo messages have been sent out when I ran the example code. Is there something that I might be missing?

    Sunday, March 24, 2013 10:16 AM


  • Th ecode below runs.  I'm ping the Localhost using the name that gets returned from Dns.GetHostName().  YOu have to enter a return for the code to finish after the ping completes.  the TTL is set to two so it will only go two hops.  You may need to increase the TTL when going through a multi-hop network.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.NetworkInformation;
    using System.Threading;
    namespace ConsoleApplication1
        class Program
            static void Main(string[] args)
                string LocalHostName = Dns.GetHostName();
        public class IP
            public static void PingIP(string IP)
                // Get an object that will block the main thread.
                AutoResetEvent waiter = new AutoResetEvent(false);
                Ping pingSender = new Ping();
                PingOptions options = new PingOptions();
                // Use the default Ttl value which is 128, 
                // but change the fragmentation behavior. 
                options.DontFragment = true;
                options.Ttl = 2;
                // When the PingCompleted event is raised,
                // the PingCompletedCallback method is called.
                pingSender.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);
                // Create a buffer of 32 bytes of data to be transmitted. 
                string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
                byte[] buffer = Encoding.ASCII.GetBytes(data);
                int timeout = 0;
                //PingReply reply = pingSender.Send(IP, timeout, buffer, options);
                pingSender.SendAsync(IP, timeout, buffer, options, waiter);
            private static void PingCompletedCallback(object sender, PingCompletedEventArgs e)
                // If the operation was canceled, display a message to the user.
                if (e.Cancelled)
                    Console.WriteLine("Ping canceled.");
                // If an error occurred, display the exception to the user.
                if (e.Error != null)
                    Console.WriteLine("Ping failed:");
                PingReply reply = e.Reply;
            public static void DisplayReply(PingReply reply)
                if (reply == null)
                Console.WriteLine("ping status: {0}", reply.Status);
                if (reply.Status == IPStatus.Success)
                    Console.WriteLine("Address: {0}", reply.Address.ToString());
                    Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
                    Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
                    Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
                    Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);


    • Edited by Joel Engineer Sunday, March 24, 2013 12:19 PM
    • Proposed as answer by Mike Feng Monday, March 25, 2013 3:41 PM
    • Marked as answer by Mike Feng Wednesday, April 3, 2013 4:52 PM
    Sunday, March 24, 2013 12:15 PM