Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Collections;
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Net.NetworkInformation;
- using System.Diagnostics;
- using System.Runtime.InteropServices;
- namespace upcping
- {
- class Program
- {
- private static List<DateTime> peaks;
- public enum CtrlTypes
- {
- CTRL_C_EVENT = 0,
- CTRL_BREAK_EVENT,
- CTRL_CLOSE_EVENT,
- CTRL_LOGOFF_EVENT = 5,
- CTRL_SHUTDOWN_EVENT
- }
- public delegate bool HandlerRoutine(CtrlTypes CtrlType);
- [DllImport("Kernel32")]
- public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
- private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
- {
- if (peaks.Count > 0)
- {
- Trace.WriteLine("\r\n\r\nPeaks:");
- foreach (var peak in peaks)
- Trace.WriteLine(peak.ToLongTimeString());
- }
- return false;
- }
- static void Main(string[] args)
- {
- string[] ips = { //"192.168.1.1", // router
- "192.168.100.1", // modem
- "84.10.148.1", // router UPC #1
- "89.75.2.161", // router UPC #2
- "213.180.146.27" }; // onet.pl
- const int interval = 1000; // w ms
- const int packetSize = 32; // w bajtach
- const int timeout = 2000; // w ms
- const int threshold = 100; // w ms
- SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);
- Ping ping = new Ping();
- var tasks = new Task<PingReply>[ips.Length];
- byte[] buffer = Encoding.ASCII.GetBytes(new string('F', packetSize));
- int row = 0;
- peaks = new List<DateTime>();
- TextWriterTraceListener textFileTracer = new TextWriterTraceListener(DateTime.Now.ToShortDateString() + " " +
- DateTime.Now.ToShortTimeString().Replace(':', '.') + ".txt");
- ConsoleTraceListener consoleTracer = new ConsoleTraceListener();
- Trace.Listeners.Add(textFileTracer);
- Trace.Listeners.Add(consoleTracer);
- Trace.AutoFlush = true;
- while (true)
- {
- if (row % 30 == 0)
- {
- Trace.Write(new string(' ', DateTime.Now.ToLongTimeString().Length));
- foreach (var ip in ips)
- Trace.Write(string.Format("{0,16}", ip));
- Trace.WriteLine("");
- row = 0;
- }
- row++;
- for (int i = 0; i < ips.Length; i++)
- tasks[i] = new Task<PingReply>((object ip) => ping.Send(ip.ToString(), timeout, buffer,
- new PingOptions() { DontFragment = true }), ips[i]);
- var start = DateTime.Now;
- foreach (var task in tasks)
- task.Start();
- try
- {
- Task.WaitAll(tasks);
- }
- catch (Exception)
- {
- Trace.WriteLine("Error");
- continue;
- }
- finally
- {
- var elapsed = interval - (int)(DateTime.Now - start).TotalMilliseconds;
- if (elapsed > 0)
- Thread.Sleep(elapsed);
- }
- StringBuilder sb = new StringBuilder();
- foreach (var task in tasks)
- {
- PingReply result = task.Result;
- if (result.Status == IPStatus.TimedOut)
- sb.AppendFormat("{0,16}", "timeout");
- else if (result.Status != IPStatus.Success)
- sb.AppendFormat("{0,16}", "*");
- else if (result.RoundtripTime >= threshold)
- {
- sb.AppendFormat("{0,16}", "**** " + result.RoundtripTime + "ms");
- DateTime lastPeak = peaks.FindLast(dateTime => (DateTime.Now - dateTime).TotalSeconds > 30);
- if (lastPeak == default(DateTime)) peaks.Add(DateTime.Now);
- }
- else
- sb.AppendFormat("{0,14}ms", (result.RoundtripTime == 0) ? "<1" : result.RoundtripTime.ToString());
- }
- for (int i = 0; i < tasks.Length; i++)
- tasks[i].Dispose();
- string log = sb.ToString();
- Trace.WriteLine(DateTime.Now.ToLongTimeString() + log);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement