Пример #1
0
        static void PingHost(Host host)
        {
            // increment the host attempts
            host.Attempts++;

            // Ping Reply class
            //https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
            var ping   = new Ping();
            var waiter = new AutoResetEvent(false);

            ping.PingCompleted += PingCompletedCallback;


            // Create a buffer of 32 bytes of data to be transmitted.
            const string data   = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            var          buffer = Encoding.ASCII.GetBytes(data);

            // Wait 12 seconds for a reply.
            const int timeout = 12000;

            // Set options for transmission:
            // The data can go through 64 gateways or routers
            // before it is destroyed, and the data packet
            // cannot be fragmented.
            var options = new PingOptions(64, true);

            // Send the ping asynchronously.
            // Use the waiter as the user token.
            // When the callback completes, it can wake up this thread.
            try
            {
                ping.SendAsync(host.Address, timeout, buffer, options, waiter);
            }
            catch
            {
                host.Exceptions++;
            }
        }
Пример #2
0
 static private void PingAll(List <string> ips)
 {
     ips.AsParallel().Select(ip => { Console.WriteLine("Try " + ip); Ping ping = new Ping(); ping.PingCompleted += Ping_PingCompleted; ping.SendAsync(ip, 2000);  return(string.Empty); });
 }