Guest User

Untitled

a guest
Jan 18th, 2017
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1. class NetworkManager
  2.     {
  3.         private const string ipBase = "10.0.0.";
  4.         private const bool resolveNames = true;
  5.  
  6.         private ConcurrentBag<Host> bag = new ConcurrentBag<Host>();
  7.         private CountdownEvent countdown;
  8.         private int upCount = 0;
  9.         private object lockObj = new object();
  10.  
  11.         public void ping()
  12.         {
  13.             ping(ipBase);
  14.         }
  15.  
  16.         public void ping(string ipBase)
  17.         {
  18.             countdown = new CountdownEvent(1);
  19.             Stopwatch sw = new Stopwatch();
  20.             sw.Start();
  21.             for (int i = 1; i < 255; i++)
  22.             {
  23.                 string ip = ipBase + i.ToString();
  24.  
  25.                 Ping p = new Ping();
  26.                 p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
  27.                 countdown.AddCount();
  28.                 p.SendAsync(ip, 1000, ip);
  29.             }
  30.             countdown.Signal();
  31.             countdown.Wait();
  32.             sw.Stop();
  33.             TimeSpan span = new TimeSpan(sw.ElapsedTicks);
  34.             Console.WriteLine("Took {0} milliseconds. {1} hosts active.", sw.ElapsedMilliseconds, upCount);
  35.             foreach (Host h in bag)
  36.             {
  37.                 Console.WriteLine(h.Name);
  38.             }
  39.             Console.ReadLine();
  40.         }
  41.  
  42.         private void p_PingCompleted(object sender, PingCompletedEventArgs e)
  43.         {
  44.             string ip = (string)e.UserState;
  45.             if (e.Reply != null && e.Reply.Status == IPStatus.Success)
  46.             {
  47.                 if (resolveNames)
  48.                 {
  49.                     string name;
  50.                     try
  51.                     {
  52.                         IPHostEntry hostEntry = Dns.GetHostEntry(ip);
  53.                         name = hostEntry.HostName;
  54.                     }
  55.                     catch (SocketException ex)
  56.                     {
  57.                         name = "?";
  58.                     }
  59.                     Console.WriteLine("{0} ({1}) is up: ({2} ms)", ip, name, e.Reply.RoundtripTime);
  60.                     bag.Add(new Host(ip, "mac", name));
  61.                 }
  62.                 else
  63.                 {
  64.                     Console.WriteLine("{0} is up: ({1} ms)", ip, e.Reply.RoundtripTime);
  65.                     bag.Add(new Host(ip, "mac", ""));
  66.                 }
  67.                 lock (lockObj)
  68.                 {
  69.                     upCount++;
  70.                 }
  71.             }
  72.             else if (e.Reply == null)
  73.             {
  74.                 Console.WriteLine("Pinging {0} failed. (Null Reply object?)", ip);
  75.             }
  76.             countdown.Signal();
  77.         }
  78.     }
Add Comment
Please, Sign In to add comment