Advertisement
andrzejiwaniuk

[Napisz sam] Program do PING'owania w sieci komputerowej

May 24th, 2013
942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5.  
  6. using System.Text;
  7. using System.Net;
  8. using System.ComponentModel;
  9. using System.Threading;
  10. using System.Net.NetworkInformation;
  11.  
  12.  
  13. namespace AndrzejIwaniuk
  14. {
  15.     class Program
  16.     {
  17.         static void Main(string[] args)
  18.         {
  19.  
  20.             if (args.Length == 0)
  21.             {
  22.                 throw new ArgumentException("Podaj adres IP/www: ");
  23.             }
  24.  
  25.             string adresWWW = args[0];
  26.             AutoResetEvent waiter = new AutoResetEvent(false);
  27.  
  28.             Ping pingSender = new System.Net.NetworkInformation.Ping();
  29.  
  30.             // When the PingCompleted event is raised,
  31.             // the PingCompletedCallback method is called.
  32.             pingSender.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);
  33.  
  34.             // Create a buffer of 32 bytes of data to be transmitted.
  35.             string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  36.             byte[] buforBajtow = Encoding.ASCII.GetBytes(data);
  37.  
  38.             int timeout = 3600;
  39.  
  40.             PingOptions opcje = new PingOptions(64, true);
  41.  
  42.             Console.WriteLine("Time to live: {0}", opcje.Ttl);
  43.             Console.WriteLine("Nie fragmentój pakietów: {0}", opcje.DontFragment);
  44.  
  45.             pingSender.SendAsync(adresWWW, timeout, buforBajtow, opcje, waiter);
  46.  
  47.             //tutaj możece umieścić jakiś kod aby coś robił w trakcje pingowania
  48.  
  49.             waiter.WaitOne();
  50.             Console.WriteLine("Polecenie Ping się skonczyło.");
  51.             Console.ReadKey();
  52.        
  53.         }
  54.    
  55.       public static void PingCompletedCallback (object sender, PingCompletedEventArgs e)
  56.         {
  57.             if (e.Cancelled)
  58.             {
  59.                 Console.WriteLine ("Ping skancelowany.");
  60.  
  61.                 ((AutoResetEvent)e.UserState).Set ();
  62.             }
  63.  
  64.              if (e.Error != null)
  65.             {
  66.                 Console.WriteLine ("Ping błąd:");
  67.                 Console.WriteLine (e.Error.ToString ());
  68.  
  69.                 ((AutoResetEvent)e.UserState).Set();
  70.             }
  71.  
  72.             PingReply odpowiedz = e.Reply;
  73.  
  74.             WyswietlOdpowiedzPing(odpowiedz);
  75.  
  76.             ((AutoResetEvent)e.UserState).Set();
  77.         }
  78.  
  79.       public static void WyswietlOdpowiedzPing(PingReply odpowiedz)
  80.       {
  81.           if (odpowiedz == null)
  82.               return;
  83.  
  84.           Console.WriteLine("Status polecenia ping: {0}", odpowiedz.Status);
  85.           if (odpowiedz.Status == IPStatus.Success)
  86.           {
  87.               Console.WriteLine("Adres IP: {0}", odpowiedz.Address.ToString());
  88.               Console.WriteLine("Time to live: {0}", odpowiedz.Options.Ttl);
  89.               Console.WriteLine("Nie jest pofragmentowany: {0}", odpowiedz.Options.DontFragment);
  90.               Console.WriteLine("Rozmiar Bufora: {0}", odpowiedz.Buffer.Length);
  91.           }
  92.       }
  93.    
  94.     }
  95.  
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement