なんてことはないのだが、そんなツールを作ってみた。
参考にさせてもらった(コピペさせてもらった???)のは
DOBON.NET さんの 「Pingを送信する」
同期で作りました
コピペと貼り付け、張り合わせで作ったので動作保障は致しかねます^^;
Form1.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
- namespace PingTool
- {
- public partial class Form1 : Form
- {
- private PingObject po = new PingObject();
- private bool endflg = true; //起動と同時にtrue。終了したらfalse。閉じてもスリープ処理中であればプロセスが完全に終わらないので。
- public Form1()
- {
- InitializeComponent();
- }
- private void Sleeping(int msec)
- {
- for (int i = 0; i < msec; i++)
- {
- Thread.Sleep(1);
- Application.DoEvents();
- if (endflg == false) { break; }//キャンセルやフォームの終了と共に処理も終了
- }
- }
- private void ping_start_button_Click(object sender, EventArgs e)
- {
- // エラーチェック
- if (IptextBox.Text == "")
- {
- MessageBox.Show("IPを入力して下さい");
- return;
- }
- // 回数初期化
- int retry = 1;//1で初期化
- if (maskedTextBox1.Enabled == true)
- {
- if (int.TryParse(maskedTextBox1.Text, out retry))
- {
- retry = int.Parse(maskedTextBox1.Text);
- }
- else
- {
- MessageBox.Show("リトライ回数には数値のみ入力可能です");
- return;
- }
- // リトライ回数は1回以上
- if (retry < 1)
- {
- MessageBox.Show("リトライ回数は1回以上です");
- return;
- }
- }
- // 初期化
- endflg = true;
- toolStripStatusLabel1.Text = "開始";
- ping_start_button.Enabled = false;
- ping_cancel_button.Enabled = true;
- PingtextBox.Text = "";
- po.TimeClear();
- // 実行
- int i = 0;
- while (endflg)
- {
- PingtextBox.AppendText(po.Ping_Start(IptextBox.Text, 3000));
- i += 1;
- // 回数指定
- if (radioButton2.Checked == true && i >= retry)
- {
- endflg = false;
- }
- else
- {
- Sleeping(100);
- }
- }
- double t = po.tsum / po.tcount;
- PingtextBox.AppendText("\r\n平均:" + t.ToString() + "\r\n");
- ping_start_button.Enabled = true;
- ping_cancel_button.Enabled = false;
- toolStripStatusLabel1.Text = "完了";
- }
- private void ping_cancel_button_Click(object sender, EventArgs e)
- {
- endflg = false;
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- endflg = false;
- }
- private void radioButton1_CheckedChanged(object sender, EventArgs e)
- {
- maskedTextBox1.Enabled = false;
- }
- private void radioButton2_CheckedChanged(object sender, EventArgs e)
- {
- maskedTextBox1.Enabled = true;
- }
- }
- }
PingObject.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net.NetworkInformation;
- using System.Collections;
- namespace PingTool
- {
- public class PingObject
- {
- public int tcount = 0;
- public int tsum = 0;
- public void TimeClear()
- {
- tcount = 0;
- tsum = 0;
- }
- // ping処理スタート
- public string Ping_Start(string ip, int timeout)
- {
- //Pingオブジェクトの作成
- Ping p = new Ping();
- //Pingを送信する
- PingReply reply = p.Send(ip, timeout);
- p.Dispose();
- //結果を取得
- if (reply.Status == IPStatus.Success)
- {
- tcount += 1;
- tsum += (int)reply.RoundtripTime;
- return "Reply from " + reply.Address + ":bytes=" + reply.Buffer.Length + " time=" + reply.RoundtripTime + "ms TTL=" + reply.Options.Ttl + "\r\n";
- }
- else
- {
- tcount += 0;
- tsum += 0;
- return "Request " + reply.Status + " \r\n";
- }
- }
- }
- }
免責
この記事やプログラムによって生じた事故・損害などは一切保証致しません。ご自身の責任でご使用ください。
子育てブログ「おとう日記」はじめました。
興味ある方、是非ご覧下さい!
おとう日記
コピペプログラマの倉庫を作りました。
サンプルプログラムなど置いておきますのでお立ち寄り下さい。
コピペプログラマ倉庫
良ければ↓投票お願いします↓ m(._.)m ペコッ

0 件のコメント:
コメントを投稿