static async Task PingAndUpdateAsync(string ip, PingResult pr) { using var ping = new System.Net.NetworkInformation.Ping(); Interlocked.Increment(ref pr.IpsPinged); var reply = await ping.SendPingAsync(ip, 500); if (reply.Status == System.Net.NetworkInformation.IPStatus.Success) { pr.OnlineIps[reply.Address] = true; } }
public static async Task <PingResult> PingAllAsync(IPAddress subnet) { var tasks = new List <Task>(); // NOTE: This is extremely lame but works for now... var baseIp = String.Join('.', subnet.ToString().Split('.').Take(3)) + "."; var pr = new PingResult(); var sw = Stopwatch.StartNew(); for (var i = 2; i < 255; i++) { var task = PingAndUpdateAsync(baseIp + i, pr); tasks.Add(task); } await Task.WhenAll(tasks).ContinueWith(t => { pr.Elapsed = sw.Elapsed; }); return(pr); }
public static PingResult PingAll(IPAddress subnet) { // NOTE: This is extremely lame but works for now... var baseIp = String.Join('.', subnet.ToString().Split('.').Take(3)) + "."; var pr = new PingResult(); var sw = Stopwatch.StartNew(); Parallel.For((long)2, 255, i => { using var p = new Ping(); Interlocked.Increment(ref pr.IpsPinged); var ip = IPAddress.Parse(baseIp + i); var result = p.Send(ip, 200); if (result?.Status == IPStatus.Success) { pr.OnlineIps[ip] = true; } }); pr.Elapsed = sw.Elapsed; return(pr); }