Chaze

ping

Dec 6th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.02 KB | None | 0 0
  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.Net;
  10. using System.Net.NetworkInformation;
  11. using System.IO;
  12. using System.Collections;
  13.  
  14. namespace WindowsFormsApplication1
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         Ping pingSender = new Ping();
  19.         IPAddress myself;
  20.         int Req_num = 0;
  21.         int Rec_num = 0;
  22.         int nRec_num = 0;
  23.  
  24.         public Form1()
  25.         {
  26.             InitializeComponent();
  27.             textBox3.BackColor = Color.White;
  28.         }
  29.  
  30.         private void button1_Click(object sender, EventArgs e)
  31.         {
  32.             IPHostEntry ihe = Dns.GetHostByName(textBox1.Text);
  33.             myself = ihe.AddressList[0];
  34.             timer1.Start();
  35.         }
  36.  
  37.         private void timer1_Tick(object sender, EventArgs e)
  38.         {
  39.  
  40.             P_component();
  41.         }
  42.         void P_component()
  43.         {
  44.             string data = RandomString(int.Parse(numericUpDown1.Value.ToString()));
  45.             byte[] buffer = Encoding.ASCII.GetBytes(data);
  46.             int timeout = 1200;
  47.             PingReply reply = pingSender.Send(myself, timeout, buffer);
  48.             if (reply.RoundtripTime != 0)
  49.             {
  50.                 textBox3.AppendText("[" + DateTime.Now.ToString("HH:mm:ss") + "] " + myself.ToString() + " " + reply.RoundtripTime.ToString() + " ms \r\n");
  51.                 Req_num++;
  52.                 req_num.Text = Req_num.ToString();
  53.                 Rec_num++;
  54.                 rec_num.Text = Rec_num.ToString();
  55.             }
  56.             else
  57.             {
  58.                 textBox3.AppendText("[" + DateTime.Now.ToString("HH:mm:ss") + "] " + myself.ToString() + " is not responding \r\n");
  59.                 Req_num++;
  60.                 req_num.Text = Req_num.ToString();
  61.                 nRec_num++;
  62.                 nrec_num.Text = nRec_num.ToString();
  63.             }
  64.  
  65.         }
  66.  
  67.         private void button2_Click(object sender, EventArgs e)
  68.         {
  69.             timer1.Stop();
  70.         }
  71.  
  72.         private void button3_Click(object sender, EventArgs e)
  73.         {
  74.             if (checkBox1.Checked)
  75.             {
  76.                 if (File.Exists("Logs"))
  77.                     saveLog();
  78.                 else
  79.                 {
  80.                     logs_Create();
  81.                     saveLog();
  82.                 }
  83.             }
  84.  
  85.             textBox3.Clear();
  86.             Req_num = 0;
  87.             req_num.Text = "0";
  88.             Rec_num = 0;
  89.             rec_num.Text = "0";
  90.             nRec_num = 0;
  91.             nrec_num.Text = "0";
  92.  
  93.  
  94.         }
  95.         private string RandomString(int size)
  96.         {
  97.             StringBuilder builder = new StringBuilder();
  98.             Random random = new Random();
  99.             char ch;
  100.             for (int i = 0; i < size; i++)
  101.             {
  102.                 ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
  103.                 builder.Append(ch);
  104.             }
  105.  
  106.             return builder.ToString();
  107.         }
  108.         private void saveLog()
  109.         {
  110.  
  111.  
  112.             string filename = "Logs\\" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".log";
  113.             System.IO.StreamWriter file = new System.IO.StreamWriter(filename);
  114.             file.WriteLine("Log was saved at: " + DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss dddd"));
  115.             file.WriteLine("Domain: " + textBox1.Text);
  116.             file.WriteLine("IP: " + myself.ToString());
  117.             file.WriteLine("Ping with " + numericUpDown1.Value.ToString() + " bytes");
  118.             file.WriteLine("Requests sent: " + Req_num.ToString());
  119.             file.WriteLine("Answer received: " + Rec_num.ToString());
  120.             file.WriteLine("Answer not received: " + nRec_num.ToString());
  121.             file.WriteLine(textBox3.Text);
  122.             file.Close();
  123.         }
  124.         private void logs_Create()
  125.         {
  126.  
  127.             Directory.CreateDirectory("Logs");
  128.  
  129.         }
  130.  
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment