diff --git a/NetPing/Form1.cs b/NetPing/Form1.cs deleted file mode 100644 index 88864a1..0000000 --- a/NetPing/Form1.cs +++ /dev/null @@ -1,258 +0,0 @@ -using System; -using System.Linq; -using System.Text; -using System.Windows.Forms; -using System.Windows.Forms.DataVisualization.Charting; -using System.Net.NetworkInformation; -using System.Threading; - -namespace NetPing -{ - public partial class frmNetPing : Form - { - public string pingHost; - public int pingTTL = 64; - public int pingTimeout = 1000; - public int display = 120; - - bool criticalErrorShown = false; - - public frmNetPing() - { - InitializeComponent(); - } - - private void button1_Click(object sender, EventArgs e) - { - if (timer1.Enabled) - { - stopPing(); - } - else - { - startPing(txtPingTarget.Text); - } - } - - public void startPing(string who, int TTL = 64, int timeout = 1000) - { - criticalErrorShown = false; - if (Uri.CheckHostName(who) == UriHostNameType.Unknown) - { - MessageBox.Show("This is not a valid host: " + who, "NetPing Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - - if (who.Length == 0) - { - MessageBox.Show("This is not a valid host!", "NetPing Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - pingHost = who; - this.Text = "NetPing - " + who; - pingTTL = Convert.ToInt32(txtTTL.Text); - pingTimeout = Convert.ToInt32(txtTimeout.Text); - display = Convert.ToInt32(txtDisplay.Text); - timer1.Interval = Convert.ToInt32(txtInterval.Text); - timer1.Enabled = true; - btnActivate.Text = "Stop"; - txtPingTarget.Enabled = false; - txtTTL.Enabled = false; - txtInterval.Enabled = false; - txtTimeout.Enabled = false; - txtDisplay.Enabled = false; - } - - public void stopPing() - { - this.Text = "NetPing"; - timer1.Enabled = false; - btnActivate.Text = "Start"; - txtPingTarget.Enabled = true; - txtTTL.Enabled = true; - txtInterval.Enabled = true; - txtTimeout.Enabled = true; - txtDisplay.Enabled = true; - } - - public void addItem(double ping, bool timeout = false) - { - - Series s = pingChart.Series["srPing"]; - double nextX = 1; - - if (s.Points.Count > 0) - { - nextX = s.Points.Last().XValue + 1; - } - - DataPoint x = new DataPoint(); - x.XValue = nextX; - x.YValues[0] = ping; - if (timeout) - { - x.IsEmpty = true; - } - - s.Points.Add(x); - - if (s.Points.Count > display) - { - s.Points.Remove(s.Points[0]); - pingChart.ResetAutoValues(); - } - } - - private void timer1_Tick(object sender, EventArgs e) - { - sendPing(pingHost, pingTTL, pingTimeout); - } - - public void sendPing(string who, int TTL = 64, int timeout = 1000) - { - AutoResetEvent waiter = new AutoResetEvent(false); - Ping pingSender = new Ping(); - 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); - - PingOptions options = new PingOptions(TTL, true); - - pingSender.SendAsync(who, timeout, buffer, options, waiter); - } - - private void PingCompletedCallback(object sender, PingCompletedEventArgs e) - { - if (e.Cancelled) - { - stopPing(); - if (!criticalErrorShown) - { - MessageBox.Show("Ping canceled"); - } - criticalErrorShown = true; - } - - if (e.Error != null) - { - stopPing(); - //this makes sure that critical errors only get shown once, to avoid getting an error of every outstanding call when a connection drops - if (!criticalErrorShown) - { - MessageBox.Show("Ping failed: " + Environment.NewLine + e.Error.ToString(), "NetPing Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - criticalErrorShown = true; - } - - PingReply reply = e.Reply; - - DisplayReply(reply); - } - - public void DisplayReply(PingReply reply) - { - if (reply == null) - { - addItem(0, true); - return; - } - - switch (reply.Status) - { - case IPStatus.Success: - addItem(reply.RoundtripTime); - break; - - case IPStatus.BadDestination: - stopPing(); - if (!criticalErrorShown) - { - MessageBox.Show("This is not a valid host!", "NetPing Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - criticalErrorShown = true; - break; - case IPStatus.BadRoute: - stopPing(); - if (!criticalErrorShown) - { - MessageBox.Show("No route to host was found!", "NetPing Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - criticalErrorShown = true; - break; - - default: //assume timeout - addItem(0, true); - break; - - } - - - - /* - 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("Buffer size: {0}", reply.Buffer.Length); - } - */ - } - - private void btnReset_Click(object sender, EventArgs e) - { - if (timer1.Enabled == false) - { - txtTTL.Text = "64"; - txtPingTarget.Text = ""; - txtInterval.Text = "1000"; - txtTimeout.Text = "1000"; - } - else - { - stopPing(); - } - - Series s = pingChart.Series["srPing"]; - s.Points.Clear(); - } - - private void txtTTL_TextChanged(object sender, EventArgs e) - { - Int32 a; - if (!Int32.TryParse(txtTTL.Text, out a)) - { - txtTTL.Text = "64"; - } - } - - private void txtTimeout_TextChanged(object sender, EventArgs e) - { - Int32 a; - if (!Int32.TryParse(txtTimeout.Text, out a)) - { - txtTimeout.Text = "1000"; - } - } - - private void txtInterval_TextChanged(object sender, EventArgs e) - { - Int32 a; - if (!Int32.TryParse(txtInterval.Text, out a)) - { - txtInterval.Text = "1000"; - } - } - - private void txtDisplay_TextChanged(object sender, EventArgs e) - { - Int32 a; - if (!Int32.TryParse(txtDisplay.Text, out a)) - { - txtDisplay.Text = "120"; - } - } - } -} diff --git a/NetPing/NetPing.csproj b/NetPing/NetPing.csproj index b78907d..88ae5d8 100644 --- a/NetPing/NetPing.csproj +++ b/NetPing/NetPing.csproj @@ -88,16 +88,17 @@ - + Form - - Form1.cs + + PingForm.cs + - - Form1.cs + + PingForm.cs ResXFileCodeGenerator diff --git a/NetPing/Form1.Designer.cs b/NetPing/PingForm.Designer.cs similarity index 91% rename from NetPing/Form1.Designer.cs rename to NetPing/PingForm.Designer.cs index 4e3f7da..4685d16 100644 --- a/NetPing/Form1.Designer.cs +++ b/NetPing/PingForm.Designer.cs @@ -1,6 +1,6 @@ namespace NetPing { - partial class frmNetPing + partial class PingForm { /// /// Required designer variable. @@ -28,15 +28,12 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend(); System.Windows.Forms.DataVisualization.Charting.LegendItem legendItem1 = new System.Windows.Forms.DataVisualization.Charting.LegendItem(); - System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series(); - System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmNetPing)); + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PingForm)); this.pingChart = new System.Windows.Forms.DataVisualization.Charting.Chart(); this.btnActivate = new System.Windows.Forms.Button(); - this.timer1 = new System.Windows.Forms.Timer(this.components); this.txtPingTarget = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); @@ -48,6 +45,8 @@ private void InitializeComponent() this.btnReset = new System.Windows.Forms.Button(); this.txtDisplay = new System.Windows.Forms.TextBox(); this.label5 = new System.Windows.Forms.Label(); + this.listBox1 = new System.Windows.Forms.ListBox(); + this.button1 = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pingChart)).BeginInit(); this.SuspendLayout(); // @@ -110,21 +109,11 @@ private void InitializeComponent() legend1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); legend1.Name = "Legend1"; this.pingChart.Legends.Add(legend1); - this.pingChart.Location = new System.Drawing.Point(9, 31); + this.pingChart.Location = new System.Drawing.Point(9, 105); this.pingChart.Margin = new System.Windows.Forms.Padding(0); this.pingChart.Name = "pingChart"; this.pingChart.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.None; - series1.BorderWidth = 2; - series1.ChartArea = "ChartArea1"; - series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline; - series1.EmptyPointStyle.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash; - series1.EmptyPointStyle.Color = System.Drawing.Color.Red; - series1.EmptyPointStyle.LegendText = "Timeout"; - series1.Legend = "Legend1"; - series1.LegendText = "Ping"; - series1.Name = "srPing"; - this.pingChart.Series.Add(series1); - this.pingChart.Size = new System.Drawing.Size(854, 348); + this.pingChart.Size = new System.Drawing.Size(854, 246); this.pingChart.TabIndex = 0; this.pingChart.Text = "Ping reply"; // @@ -145,10 +134,6 @@ private void InitializeComponent() this.btnActivate.UseVisualStyleBackColor = false; this.btnActivate.Click += new System.EventHandler(this.button1_Click); // - // timer1 - // - this.timer1.Tick += new System.EventHandler(this.timer1_Tick); - // // txtPingTarget // this.txtPingTarget.AccessibleDescription = "Insert IP address or hostname"; @@ -337,12 +322,41 @@ private void InitializeComponent() this.label5.TabIndex = 11; this.label5.Text = "Display:"; // - // frmNetPing + // listBox1 + // + this.listBox1.BackColor = System.Drawing.Color.Black; + this.listBox1.ForeColor = System.Drawing.Color.White; + this.listBox1.FormattingEnabled = true; + this.listBox1.Location = new System.Drawing.Point(9, 33); + this.listBox1.Name = "listBox1"; + this.listBox1.Size = new System.Drawing.Size(798, 69); + this.listBox1.TabIndex = 13; + // + // button1 + // + this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.button1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); + this.button1.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); + this.button1.FlatAppearance.BorderSize = 0; + this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.button1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(255))))); + this.button1.Location = new System.Drawing.Point(813, 34); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(50, 68); + this.button1.TabIndex = 14; + this.button1.Text = "Stop"; + this.button1.UseVisualStyleBackColor = false; + this.button1.Click += new System.EventHandler(this.button1_Click_1); + // + // PingForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.Color.Black; - this.ClientSize = new System.Drawing.Size(872, 388); + this.ClientSize = new System.Drawing.Size(872, 360); + this.Controls.Add(this.button1); + this.Controls.Add(this.listBox1); this.Controls.Add(this.txtDisplay); this.Controls.Add(this.label5); this.Controls.Add(this.btnReset); @@ -358,7 +372,7 @@ private void InitializeComponent() this.Controls.Add(this.pingChart); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MinimumSize = new System.Drawing.Size(640, 320); - this.Name = "frmNetPing"; + this.Name = "PingForm"; this.RightToLeft = System.Windows.Forms.RightToLeft.No; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "NetPing"; @@ -372,7 +386,6 @@ private void InitializeComponent() private System.Windows.Forms.DataVisualization.Charting.Chart pingChart; private System.Windows.Forms.Button btnActivate; - private System.Windows.Forms.Timer timer1; private System.Windows.Forms.TextBox txtPingTarget; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; @@ -384,6 +397,8 @@ private void InitializeComponent() private System.Windows.Forms.Button btnReset; private System.Windows.Forms.TextBox txtDisplay; private System.Windows.Forms.Label label5; + private System.Windows.Forms.ListBox listBox1; + private System.Windows.Forms.Button button1; } } diff --git a/NetPing/PingForm.cs b/NetPing/PingForm.cs new file mode 100644 index 0000000..0ece395 --- /dev/null +++ b/NetPing/PingForm.cs @@ -0,0 +1,112 @@ +using System; +using System.Windows.Forms; +using System.ComponentModel; +using System.Windows.Forms.DataVisualization.Charting; +using System.Linq; + +namespace NetPing +{ + public partial class PingForm : Form + { + public string pingHost; + public int pingTTL = 64; + public int pingTimeout = 1000; + public int display = 120; + + private BindingList targets = new BindingList(); + + public PingForm() + { + InitializeComponent(); + + listBox1.DataSource = targets; + listBox1.DisplayMember = "Target"; + } + + private void button1_Click(object sender, EventArgs e) + { + if (Uri.CheckHostName(txtPingTarget.Text) == UriHostNameType.Unknown) + { + MessageBox.Show("This is not a valid host: " + txtPingTarget.Text, "NetPing Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (txtPingTarget.Text.Length == 0) + { + MessageBox.Show("This is not a valid host!", "NetPing Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + pingTTL = Convert.ToInt32(txtTTL.Text); + pingTimeout = Convert.ToInt32(txtTimeout.Text); + display = Convert.ToInt32(txtDisplay.Text); + + targets.Add(new PingTarget(this, txtPingTarget.Text, pingTTL, pingTimeout, 1000, display)); + } + + + private void btnReset_Click(object sender, EventArgs e) + { + foreach (PingTarget _target in targets.ToList()) + { + _target.Stop(); + } + targets.Clear(); + } + private void txtTTL_TextChanged(object sender, EventArgs e) + { + int a; + if (!int.TryParse(txtTTL.Text, out a)) + { + txtTTL.Text = "64"; + } + } + + private void txtTimeout_TextChanged(object sender, EventArgs e) + { + int a; + if (!int.TryParse(txtTimeout.Text, out a)) + { + txtTimeout.Text = "1000"; + } + } + + private void txtInterval_TextChanged(object sender, EventArgs e) + { + int a; + if (!int.TryParse(txtInterval.Text, out a)) + { + txtInterval.Text = "1000"; + } + } + + private void txtDisplay_TextChanged(object sender, EventArgs e) + { + int a; + if (!int.TryParse(txtDisplay.Text, out a)) + { + txtDisplay.Text = "120"; + } + } + + private void button1_Click_1(object sender, EventArgs e) + { + if (listBox1.SelectedItem == null) { return; } + PingTarget _target = (PingTarget)listBox1.SelectedItem; + _target.Stop(); + targets.Remove(_target); + } + + public void Remove(PingTarget _t) + { + targets.Remove(_t); + } + + public Chart Chart + { + get + { + return pingChart; + } + } + } +} diff --git a/NetPing/Form1.resx b/NetPing/PingForm.resx similarity index 99% rename from NetPing/Form1.resx rename to NetPing/PingForm.resx index 96f6dde..c7a013a 100644 --- a/NetPing/Form1.resx +++ b/NetPing/PingForm.resx @@ -117,9 +117,6 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - 17, 17 - diff --git a/NetPing/PingTarget.cs b/NetPing/PingTarget.cs new file mode 100644 index 0000000..96a8c83 --- /dev/null +++ b/NetPing/PingTarget.cs @@ -0,0 +1,187 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.NetworkInformation; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Timers; +using System.Windows.Forms; +using System.Windows.Forms.DataVisualization.Charting; + +namespace NetPing +{ + public class PingTarget + { + private PingForm _form; + private Chart _chart; + private string _target; + private int _interval; + private int _timeout; + private int _ttl; + private int _display; + private Series _serie = new Series(); + private System.Timers.Timer _timer; + private Guid _guid; + + public string Target + { + get + { + return _target; + } + } + + public string Guid + { + get + { + return _guid.ToString(); + } + } + + public PingTarget(PingForm form, string target, int TTL = 64, int timeout = 1000, int interval = 1000, int display = 100) + { + _guid = System.Guid.NewGuid(); + _target = target; + _chart = form.Chart; + _form = form; + _timeout = timeout; + _interval = interval; + _ttl = TTL; + _display = display; + + _timer = new System.Timers.Timer(); + _timer.Interval = _interval; + _timer.Enabled = true; + _timer.Elapsed += new ElapsedEventHandler(OnTimedEvent); + + _serie.BorderWidth = 2; + _serie.ChartArea = "ChartArea1"; + _serie.ChartType = SeriesChartType.Spline; + _serie.EmptyPointStyle.BorderDashStyle = ChartDashStyle.Dash; + _serie.EmptyPointStyle.Color = System.Drawing.Color.Red; + _serie.EmptyPointStyle.LegendText = "Timeout"; + _serie.Legend = "Legend1"; + _serie.LegendText = "Ping " + target; + _serie.Name = "Ping" + _guid.ToString(); + + _chart.Series.Add(_serie); + } + + public void Stop() + { + _timer.Enabled = false; + MethodInvoker mi = delegate + { + _chart.Series.Remove(_serie); + }; + _chart.Invoke(mi); + + _form.Remove(this); + } + + private void OnTimedEvent(object source, ElapsedEventArgs e) + { + sendPing(_target, _ttl, _timeout); + } + + private void sendPing(string who, int TTL = 64, int timeout = 1000) + { + AutoResetEvent waiter = new AutoResetEvent(false); + Ping pingSender = new Ping(); + 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); + + PingOptions options = new PingOptions(TTL, true); + + pingSender.SendAsync(who, timeout, buffer, options, waiter); + } + public void addItem(double ping, bool timeout = false) + { + + Series s = _serie; + double nextX = 1; + + if (s.Points.Count > 0) + { + nextX = s.Points.Last().XValue + 1; + } + + DataPoint x = new DataPoint(); + x.XValue = nextX; + x.YValues[0] = ping; + if (timeout) + { + x.IsEmpty = true; + } + + MethodInvoker mi = delegate + { + s.Points.Add(x); + + if (s.Points.Count > _display) + { + s.Points.Remove(s.Points[0]); + _chart.ResetAutoValues(); + } + }; + _chart.Invoke(mi); + } + private void PingCompletedCallback(object sender, PingCompletedEventArgs e) + { + if (e.Cancelled || e.Error != null) + { + Stop(); + + if (e.Cancelled) + { + MessageBox.Show("Ping canceled"); + } + else if (e.Error != null) + { + MessageBox.Show("Ping failed: " + Environment.NewLine + e.Error.ToString(), "NetPing Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + return; + } + + PingReply reply = e.Reply; + + if (reply == null) + { + addItem(0, true); + return; + } + + Random rnd = new Random(); + + switch (reply.Status) + { + case IPStatus.Success: + + addItem(reply.RoundtripTime); + + break; + + case IPStatus.BadDestination: + Stop(); + MessageBox.Show("This is not a valid host!", "NetPing Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + break; + case IPStatus.BadRoute: + Stop(); + MessageBox.Show("No route to host was found!", "NetPing Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + break; + + default: + addItem(0, true); + break; + + } + + } + } +} diff --git a/NetPing/Program.cs b/NetPing/Program.cs index c51a0cb..6b9841c 100644 --- a/NetPing/Program.cs +++ b/NetPing/Program.cs @@ -13,7 +13,7 @@ static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new frmNetPing()); + Application.Run(new PingForm()); } } } diff --git a/NetPing/Properties/AssemblyInfo.cs b/NetPing/Properties/AssemblyInfo.cs index 8972300..dce3652 100644 --- a/NetPing/Properties/AssemblyInfo.cs +++ b/NetPing/Properties/AssemblyInfo.cs @@ -32,7 +32,7 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.1.0")] -[assembly: AssemblyFileVersion("1.0.1.0")] +[assembly: AssemblyVersion("1.2.0.0")] +[assembly: AssemblyFileVersion("1.2.0.0")] [assembly: NeutralResourcesLanguage("en")]