2009年6月8日月曜日

C# で Ping送信ツール

C# で Ping送信ツールを作ってみよう!

なんてことはないのだが、そんなツールを作ってみた。
参考にさせてもらった(コピペさせてもらった???)のは

DOBON.NET さんの 「Pingを送信する」

同期で作りました
コピペと貼り付け、張り合わせで作ったので動作保障は致しかねます^^;

Form1.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Threading;  
  10.   
  11. namespace PingTool  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.   
  16.         private PingObject po = new PingObject();  
  17.         private bool endflg = true//起動と同時にtrue。終了したらfalse。閉じてもスリープ処理中であればプロセスが完全に終わらないので。  
  18.   
  19.         public Form1()  
  20.         {  
  21.             InitializeComponent();  
  22.         }  
  23.   
  24.         private void Sleeping(int msec)  
  25.         {  
  26.             for (int i = 0; i < msec; i++)  
  27.             {  
  28.                 Thread.Sleep(1);  
  29.                 Application.DoEvents();  
  30.                 if (endflg == false) { break; }//キャンセルやフォームの終了と共に処理も終了  
  31.             }  
  32.         }  
  33.   
  34.         private void ping_start_button_Click(object sender, EventArgs e)  
  35.         {  
  36.             // エラーチェック  
  37.             if (IptextBox.Text == "")  
  38.             {  
  39.                 MessageBox.Show("IPを入力して下さい");  
  40.                 return;  
  41.             }  
  42.             // 回数初期化  
  43.             int retry = 1;//1で初期化  
  44.             if (maskedTextBox1.Enabled == true)  
  45.             {  
  46.                 if (int.TryParse(maskedTextBox1.Text, out retry))  
  47.                 {  
  48.                     retry = int.Parse(maskedTextBox1.Text);  
  49.                 }  
  50.                 else  
  51.                 {  
  52.                     MessageBox.Show("リトライ回数には数値のみ入力可能です");  
  53.                     return;  
  54.                 }  
  55.                 // リトライ回数は1回以上  
  56.                 if (retry < 1)  
  57.                 {  
  58.                     MessageBox.Show("リトライ回数は1回以上です");  
  59.                     return;  
  60.                 }  
  61.             }  
  62.   
  63.             // 初期化  
  64.             endflg = true;  
  65.             toolStripStatusLabel1.Text = "開始";  
  66.             ping_start_button.Enabled = false;  
  67.             ping_cancel_button.Enabled = true;  
  68.             PingtextBox.Text = "";  
  69.             po.TimeClear();  
  70.   
  71.             // 実行  
  72.             int i = 0;  
  73.             while (endflg)  
  74.             {  
  75.                 PingtextBox.AppendText(po.Ping_Start(IptextBox.Text, 3000));  
  76.                 i += 1;  
  77.                 // 回数指定  
  78.                 if (radioButton2.Checked == true && i >= retry)  
  79.                 {  
  80.                     endflg = false;  
  81.                 }  
  82.                 else  
  83.                 {  
  84.                     Sleeping(100);  
  85.                 }  
  86.             }  
  87.             double t = po.tsum / po.tcount;  
  88.             PingtextBox.AppendText("\r\n平均:" + t.ToString() + "\r\n");  
  89.             ping_start_button.Enabled = true;  
  90.             ping_cancel_button.Enabled = false;  
  91.             toolStripStatusLabel1.Text = "完了";  
  92.         }  
  93.   
  94.         private void ping_cancel_button_Click(object sender, EventArgs e)  
  95.         {  
  96.             endflg = false;  
  97.         }  
  98.   
  99.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
  100.         {  
  101.             endflg = false;  
  102.         }  
  103.   
  104.         private void radioButton1_CheckedChanged(object sender, EventArgs e)  
  105.         {  
  106.             maskedTextBox1.Enabled = false;  
  107.         }  
  108.   
  109.         private void radioButton2_CheckedChanged(object sender, EventArgs e)  
  110.         {  
  111.             maskedTextBox1.Enabled = true;  
  112.         }  
  113.     }  
  114. }  

PingObject.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Net.NetworkInformation;  
  6. using System.Collections;  
  7.   
  8. namespace PingTool  
  9. {  
  10.     public class PingObject  
  11.     {  
  12.         public int tcount = 0;  
  13.         public int tsum = 0;  
  14.   
  15.         public void TimeClear()  
  16.         {  
  17.             tcount = 0;  
  18.             tsum = 0;  
  19.         }  
  20.   
  21.         // ping処理スタート  
  22.         public string Ping_Start(string ip, int timeout)  
  23.         {  
  24.             //Pingオブジェクトの作成  
  25.             Ping p = new Ping();  
  26.             //Pingを送信する  
  27.             PingReply reply = p.Send(ip, timeout);  
  28.             p.Dispose();  
  29.             //結果を取得  
  30.             if (reply.Status == IPStatus.Success)  
  31.             {  
  32.                 tcount += 1;  
  33.                 tsum += (int)reply.RoundtripTime;  
  34.                 return "Reply from " + reply.Address + ":bytes=" + reply.Buffer.Length + " time=" + reply.RoundtripTime + "ms TTL=" + reply.Options.Ttl + "\r\n";  
  35.             }  
  36.             else  
  37.             {  
  38.                 tcount += 0;  
  39.                 tsum += 0;  
  40.                 return "Request " + reply.Status + " \r\n";  
  41.             }  
  42.         }  
  43.     }  
  44. }  




免責
この記事やプログラムによって生じた事故・損害などは一切保証致しません。ご自身の責任でご使用ください。

子育てブログ「おとう日記」はじめました。
興味ある方、是非ご覧下さい!
おとう日記

コピペプログラマの倉庫を作りました。
サンプルプログラムなど置いておきますのでお立ち寄り下さい。
コピペプログラマ倉庫


良ければ↓投票お願いします↓ m(._.)m ペコッ
人気ブログランキングへ

0 件のコメント:

コメントを投稿