如何更有效地从2个IP地址计算IP数量?
我正在开发 ping 应用程序,其中有一个进度条,显示范围 IP 的 ping 进度。
这就是我计算要设置为进度条最大值的 IP 范围的方法:
pbProgress.Maximum = 1 + (IPAddress.NetworkToHostOrder
(BitConverter.ToInt32(IPAddress.Parse(txtTo.Text).GetAddressBytes(), 0)) -
IPAddress.NetworkToHostOrder(BitConverter.ToInt32
(IPAddress.Parse(txtFrom.Text).GetAddressBytes(), 0)));
这里的问题是我将范围从 0.0.0.0 开始到 1.0.0.0 计算该范围需要花费大量时间,因此通过该值为进度条最大值。有没有更好的解决方案可以更快地计算IP数量?
我总是可以对 ping 的 IP 地址进行限制,例如 10k 个地址,但我只是不想限制该程序的任何功能。
有什么想法吗?
I am working on pinging app in which I have a progress bar which displays progress of pinging of range IPs.
This is how I calculate the range of IPs to set as maximum value for the progress bar:
pbProgress.Maximum = 1 + (IPAddress.NetworkToHostOrder
(BitConverter.ToInt32(IPAddress.Parse(txtTo.Text).GetAddressBytes(), 0)) -
IPAddress.NetworkToHostOrder(BitConverter.ToInt32
(IPAddress.Parse(txtFrom.Text).GetAddressBytes(), 0)));
The problem here is that I put range starting from 0.0.0.0 to 1.0.0.0 it takes an awful amount of time to calculate the range, therefore passing the value to the progress bar maximum value. Is there any better solution to calculate the number of IPs quicker?
I could always put a limit of IP addresses to ping for example to 10k addresses, but I just don't want to limit any functionality of this program.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是您正在创建一些包含每个 IP 地址的数据结构,例如列表或数组。您需要做的就是使用
for
循环之类的方法迭代它们,以便根据需要生成它们,而不是在开始之前生成。由于您想使用多个线程,请尝试使用类似
Parallel.For< /代码>
。
Your problem is that you're creating some data structure, like a list or array, containing each of the IP addresses. All you need to do is iterate over them with something like a
for
loop so they are generated as needed rather than before you start.Since you want to use multiple threads, try using something like
Parallel.For
.