让我解释一下我试图用这个程序做什么。我们的网络上有26个不同的位置。我们的IP地址设置方式因地点而异。对于我们的公司办公室,所有IP地址都是10.1。?。?在另一个地方它将是10.2。?。?通过每个位置等等。然后,我们将每台PC标记为一个数字,IP地址与该PC编号对应。所以我的电脑是10.1.2.98因为我在PC298的公司办公室。我正在构建一个程序,允许我输入一个潜在的PC号码并ping每个位置,以查看该IP号是否可用于该PC号码。因此,例如,我将输入466,程序将能够ping每个位置。它将ping 10.1.4.66然后它将ping 10.2.4.66,依此类推。我找不到任何类似的东西;所以目前我只有程序ping分支机构分隔的网络上的当前IP地址。当我真的只需要ping 26个地址来测试它时,以这种方式ping所有这些地址需要很长时间。我希望能够查找开放地址,以便我们可以在不使用相同IP地址的情况下将新PC添加到该位置。有什么想法吗?
以下是我当前设置扫描其中一个位置,以便您可以看到我当前正在该程序中执行ping操作。但是,我知道为了输入用户请求,需要进行大量更改。
public _8thStreet()
{
InitializeComponent();
hostArray = new String[27];
// Set our ping amt
amt_ping = 2;
// Enter our hosts
hostArray[0] = "10.2.4.49";
hostArray[1] = "10.2.4.50";
hostArray[2] = "10.2.4.51";
hostArray[3] = "10.2.4.52";
hostArray[4] = "10.2.4.53";
hostArray[5] = "10.2.4.54";
hostArray[6] = "10.2.4.55";
hostArray[7] = "10.2.4.56";
hostArray[8] = "10.2.4.57";
hostArray[9] = "10.2.4.58";
hostArray[10] = "10.2.4.59";
hostArray[11] = "10.2.4.60";
hostArray[12] = "10.2.4.61";
hostArray[13] = "10.2.4.62";
hostArray[14] = "10.2.4.63";
hostArray[15] = "10.2.4.64";
hostArray[16] = "10.2.4.65";
hostArray[17] = "10.2.4.66";
hostArray[18] = "10.2.4.67";
hostArray[19] = "10.2.4.68";
hostArray[20] = "10.2.4.69";
hostArray[21] = "10.2.4.70";
hostArray[22] = "10.2.4.71";
hostArray[23] = "10.2.4.72";
hostArray[24] = "10.2.4.73";
hostArray[25] = "10.2.4.74";
hostArray[26] = "10.2.4.75";
}
private void ping_hosts()
{
try
{
// Disable our button
btn_ping.Enabled = false;
// Clear our list view
lv_results.Items.Clear();
// Cycle through our host array
foreach (String host in hostArray)
{
// Write our status
lbl_status.Text = "Pinging (x" + amt_ping + "): " + host;
// Allow the GUI to update
Application.DoEvents();
// Ping the host four times
double loss = get_loss(host, amt_ping);
// Determine if there is any loss
if (loss > 0)
{
// Insert into the List View
ListViewItem lv = lv_results.Items.Insert(lv_results.Items.Count, host);
lv.SubItems.Add(Convert.ToString(loss) + "%");
} // End If
else
{
//Insert into the List View
ListViewItem lv = lv_results.Items.Insert(lv_results.Items.Count, host);
lv.SubItems.Add(Convert.ToString(loss) + "%");
}
} // End foreach
// Update our label
lbl_status.Text = "Complete - press Ping to restart.";
// Enable our button
btn_ping.Enabled = true;
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
private void btn_ping_Click(object sender, EventArgs e)
{
ping_hosts();
}
private double get_loss(String host, int pingAmount)
{
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
options.DontFragment = true;
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
int failed = 0;
// Loop the amount of times to ping
for (int i = 0; i < pingAmount; i++)
{
PingReply reply = pingSender.Send(host, timeout, buffer, options);
if (reply.Status != IPStatus.Success)
{
failed += 1;
}
// Allow the GUI to update
Application.DoEvents();
} // End For
// Return the percentage
double percent = (failed / pingAmount) * 100;
return percent;
}
}
答案 0 :(得分:1)
由于要ping的IP数量不多,您可以创建任务以异步ping客户端。
string[] ips = new string[] { "192.168.1.1", "192.168.1.50" , "192.168.1.100" };
var pingTasks = ips.Select(address => new Ping().SendTaskAsync(address));
var replies = await Task.WhenAll(pingTasks);
var alives = replies.Where(r => r.Reply.Status == IPStatus.Success)
.Select(r=>r.Address)
.ToList();