Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Text;
- using System.Net;
- using System.ComponentModel;
- using System.Threading;
- using System.Net.NetworkInformation;
- namespace AndrzejIwaniuk
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length == 0)
- {
- throw new ArgumentException("Podaj adres IP/www: ");
- }
- string adresWWW = args[0];
- AutoResetEvent waiter = new AutoResetEvent(false);
- Ping pingSender = new System.Net.NetworkInformation.Ping();
- // When the PingCompleted event is raised,
- // the PingCompletedCallback method is called.
- pingSender.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);
- // Create a buffer of 32 bytes of data to be transmitted.
- string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
- byte[] buforBajtow = Encoding.ASCII.GetBytes(data);
- int timeout = 3600;
- PingOptions opcje = new PingOptions(64, true);
- Console.WriteLine("Time to live: {0}", opcje.Ttl);
- Console.WriteLine("Nie fragmentój pakietów: {0}", opcje.DontFragment);
- pingSender.SendAsync(adresWWW, timeout, buforBajtow, opcje, waiter);
- //tutaj możece umieścić jakiś kod aby coś robił w trakcje pingowania
- waiter.WaitOne();
- Console.WriteLine("Polecenie Ping się skonczyło.");
- Console.ReadKey();
- }
- public static void PingCompletedCallback (object sender, PingCompletedEventArgs e)
- {
- if (e.Cancelled)
- {
- Console.WriteLine ("Ping skancelowany.");
- ((AutoResetEvent)e.UserState).Set ();
- }
- if (e.Error != null)
- {
- Console.WriteLine ("Ping błąd:");
- Console.WriteLine (e.Error.ToString ());
- ((AutoResetEvent)e.UserState).Set();
- }
- PingReply odpowiedz = e.Reply;
- WyswietlOdpowiedzPing(odpowiedz);
- ((AutoResetEvent)e.UserState).Set();
- }
- public static void WyswietlOdpowiedzPing(PingReply odpowiedz)
- {
- if (odpowiedz == null)
- return;
- Console.WriteLine("Status polecenia ping: {0}", odpowiedz.Status);
- if (odpowiedz.Status == IPStatus.Success)
- {
- Console.WriteLine("Adres IP: {0}", odpowiedz.Address.ToString());
- Console.WriteLine("Time to live: {0}", odpowiedz.Options.Ttl);
- Console.WriteLine("Nie jest pofragmentowany: {0}", odpowiedz.Options.DontFragment);
- Console.WriteLine("Rozmiar Bufora: {0}", odpowiedz.Buffer.Length);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement