Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net.NetworkInformation;
- using System.Runtime.InteropServices;
- using System.Threading;
- namespace ErmineMD.CrazyPingTest
- {
- class Program
- {
- #region Trap application termination
- // https://stackoverflow.com/questions/474679/capture-console-exit-c-sharp
- [DllImport("Kernel32")]
- private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);
- private delegate bool EventHandler(CtrlType sig);
- static EventHandler exitHandler;
- enum CtrlType
- {
- CTRL_C_EVENT = 0,
- CTRL_BREAK_EVENT = 1,
- CTRL_CLOSE_EVENT = 2,
- CTRL_LOGOFF_EVENT = 5,
- CTRL_SHUTDOWN_EVENT = 6
- }
- static bool ExitHandler(CtrlType sig)
- {
- exiting = true;
- needToExit.WaitOne();
- needToExit.Dispose();
- return true;
- }
- #endregion
- static Random rand = new Random();
- static object randLocker = new object();
- static SemaphoreSlim semaphore = new SemaphoreSlim(300, 300);
- static bool exiting = false;
- static CountdownEvent countdown = new CountdownEvent(1);
- static AutoResetEvent needToExit = new AutoResetEvent(false);
- static int j = 1;
- static int index = 0;
- // $a = 1..254 | % {([string]$_).padleft(3)} | get-random -count 254; for ($i = 0; $i -lt $a.length; $i += 16) { ($a[$i..($i+15)] -join ', ') + ',' }
- static int[] array = {
- 205, 106, 145, 54, 227, 38, 240, 22, 84, 249, 197, 61, 251, 231, 250, 103,
- 112, 86, 47, 81, 136, 242, 64, 87, 222, 50, 19, 110, 125, 1, 244, 253,
- 25, 144, 93, 52, 90, 4, 99, 16, 243, 129, 164, 160, 188, 190, 69, 206,
- 44, 83, 79, 9, 209, 10, 23, 95, 175, 207, 89, 161, 39, 163, 141, 122,
- 28, 213, 24, 111, 32, 12, 174, 123, 56, 30, 155, 237, 181, 196, 116, 121,
- 166, 128, 27, 217, 62, 192, 34, 2, 194, 168, 224, 13, 33, 203, 88, 15,
- 186, 219, 3, 210, 176, 204, 130, 171, 58, 6, 94, 211, 232, 208, 150, 36,
- 179, 100, 51, 53, 254, 92, 235, 80, 228, 85, 5, 170, 35, 137, 49, 162,
- 115, 173, 191, 199, 183, 238, 225, 73, 42, 154, 77, 76, 133, 113, 119, 214,
- 195, 167, 229, 126, 153, 169, 46, 82, 124, 71, 63, 117, 236, 218, 220, 165,
- 21, 245, 68, 135, 198, 234, 247, 157, 96, 156, 193, 233, 127, 143, 159, 17,
- 230, 109, 70, 60, 65, 146, 40, 98, 182, 20, 101, 215, 41, 200, 59, 104,
- 216, 239, 14, 78, 43, 148, 67, 108, 29, 185, 57, 66, 11, 212, 75, 131,
- 184, 31, 91, 114, 134, 152, 105, 132, 189, 140, 37, 72, 18, 178, 26, 149,
- 151, 138, 202, 147, 172, 158, 55, 248, 241, 118, 142, 8, 107, 201, 97, 187,
- 177, 74, 226, 246, 139, 102, 120, 223, 45, 7, 252, 221, 48, 180 };
- static int arrayLen = array.Length;
- public static void MyPing()
- {
- int i;
- // Variant 1 - random order IP adresses
- /*lock (randLocker)
- i = rand.Next(1, 254);*/
- // Variant 2 - normal order IP adresses
- /*lock (randLocker)
- {
- i = j;
- if (j == 50)
- j = 1;
- else
- ++j;
- }*/
- // Variant 3 - fixed array of random IP adresses
- lock (randLocker)
- {
- i = array[index];
- if (index == arrayLen - 1)
- index = 0;
- else
- ++index;
- }
- string ipAddress = "192.168.1." + i;
- using (var ping = new Ping())
- {
- try
- {
- Console.WriteLine(ipAddress.PadRight(20) + ">");
- var reply = ping.Send(ipAddress, 1000);
- Console.WriteLine(ipAddress.PadRight(20) + reply.Status.ToString());
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error!!! " + ipAddress + " " + ex.Message);
- }
- }
- semaphore.Release();
- countdown.Signal();
- }
- static void Main(string[] args)
- {
- exitHandler += new EventHandler(ExitHandler);
- SetConsoleCtrlHandler(exitHandler, true);
- while (!exiting)
- {
- semaphore.Wait();
- countdown.AddCount();
- //Thread.Sleep(10);
- Thread t = new Thread(MyPing);
- t.Start();
- }
- countdown.Signal();
- countdown.Wait();
- countdown.Dispose();
- semaphore.Dispose();
- // Not necessary
- Console.WriteLine("--------------------------------------------------------- Exit ----");
- Thread.Sleep(1000);
- needToExit.Set();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement