/
FormMain.cs
109 lines (88 loc) · 2.99 KB
/
FormMain.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Synchronous_Ping_Sweep
{
public partial class FormMain : Form
{
PingSweep pingSweep = new PingSweep();
public FormMain()
{
InitializeComponent();
}
private void buttonPingSweep_Click(object sender, EventArgs e)
{
buttonSweepSync.Enabled = false;
pingSweep.RunPingSweep_Sync();
buttonSweepSync.Enabled = true;
}
private void buttonSweepAsync_Click(object sender, EventArgs e)
{
buttonSweepSync.Enabled = false;
pingSweep.RunPingSweep_Async();
buttonSweepSync.Enabled = true;
}
}
public class PingSweep
{
private string BaseIP = "192.168.1.";
private int StartIP = 1;
private int StopIP = 255;
private string ip;
private int timeout = 100;
private int nFound = 0;
static object lockObj = new object();
Stopwatch stopWatch = new Stopwatch();
TimeSpan ts;
public void RunPingSweep_Sync()
{
nFound = 0;
stopWatch.Start();
System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
for (int i = StartIP; i <= StopIP; i++)
{
ip = BaseIP + i.ToString();
System.Net.NetworkInformation.PingReply rep = p.Send(ip, timeout);
if (rep.Status == System.Net.NetworkInformation.IPStatus.Success)
{
nFound++;
}
}
stopWatch.Stop();
ts = stopWatch.Elapsed;
MessageBox.Show(nFound.ToString() + " devices found! Elapsed time: " + ts.ToString(), "Synchronous");
}
public async void RunPingSweep_Async()
{
nFound = 0;
var tasks = new List<Task>();
stopWatch.Start();
for (int i = StartIP; i <= StopIP; i++)
{
ip = BaseIP + i.ToString();
System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
var task = PingAndUpdateAsync(p, ip);
tasks.Add(task);
}
await Task.WhenAll(tasks).ContinueWith(t =>
{
stopWatch.Stop();
ts = stopWatch.Elapsed;
MessageBox.Show(nFound.ToString() + " devices found! Elapsed time: " + ts.ToString(), "Asynchronous");
});
}
private async Task PingAndUpdateAsync(System.Net.NetworkInformation.Ping ping, string ip)
{
var reply = await ping.SendPingAsync(ip, timeout);
if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
{
lock(lockObj)
{
nFound++;
}
}
}
}
}