ping 大量 IP 地址的最快方法是什么?
我的数据表中有大量 IP 地址列表,我必须快速对它们执行 ping 操作, 我使用了这段代码:
public bool PingIP(string IP)
{
bool result = false;
try
{
Ping ping = new Ping();
PingReply pingReply = ping.Send(IP);
if (pingReply.Status == IPStatus.Success)
result = true;
}
catch
{
result = false;
}
return result;
}
然后我在 while 循环中调用它:
private void CheckNetworkState()
{
while (rowindexping > -1)
{
if (rowindexping == tbl_ClientInfo.Rows.Count)
{
rowindexping = -1;
return;
}
string ip = tbl_ClientInfo.Rows[rowindexping]["clientip"].ToString();
if (!PingIP(ip))
{
do something
}
rowindexping++;
Thread.Sleep(100);
}
}
因为我想在项目的后台完成这项工作,所以我在线程中调用该类:
threadping = new Thread(CheckNetworkState);
threadping.IsBackground = true;
threadping.Start();
我的问题是它需要很多时间并且不能在后台工作。我的意思是系统正忙,直到 tbl_clientinfo 中的所有 IP 地址都通过 ping 类。 我希望该系统检查所有行,因为我正在处理项目的其他部分。
我做对了吗?
I have a large list of IP addresses in a datatable and i have to ping them so quickly,
I used this code :
public bool PingIP(string IP)
{
bool result = false;
try
{
Ping ping = new Ping();
PingReply pingReply = ping.Send(IP);
if (pingReply.Status == IPStatus.Success)
result = true;
}
catch
{
result = false;
}
return result;
}
then i call it in while loop :
private void CheckNetworkState()
{
while (rowindexping > -1)
{
if (rowindexping == tbl_ClientInfo.Rows.Count)
{
rowindexping = -1;
return;
}
string ip = tbl_ClientInfo.Rows[rowindexping]["clientip"].ToString();
if (!PingIP(ip))
{
do something
}
rowindexping++;
Thread.Sleep(100);
}
}
Since i want to do this work at the background of my project i call the class in a thread:
threadping = new Thread(CheckNetworkState);
threadping.IsBackground = true;
threadping.Start();
my problem is that it takes so many time and does not work at the background. i mean the system is busy till all ip addresses in tbl_clientinfo go through the ping class.
i want that system check all rows since i'm working with other part of my project.
Did i do correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码在单个线程上运行所有代码;你没有使用多线程。另外,为什么里面有一个 Thread.Sleep ?
请尝试以下操作:
所有
IPPingIP
注意:您还可以在每次从数据库获取新行时启动一个新线程
示例:
Your code is running all the code on a single thread; you're not using multiple threads. Also, why do you have a
Thread.Sleep
in there?Try the following:
all
the IPsPingIP
for each IP addressNote: You can also spin up a new thread every time you get a new row from the database
Sample:
我要做的是在后台运行一个单独的多线程守护进程,执行 ping 操作,并将结果放入数据库中。有一个稍后通过 AJAX 加载结果的页面。
What I would do is have a separate multi-threaded daemon running in the background, doing the pings, and putting the results in a database. Have a page that loads results via AJAX later.
考虑对 fping 实用程序进行系统调用。当然,它不是托管代码,但 fping 针对您的特定任务进行了很好的优化,并将您的问题简化为单个调用,然后处理基于文本的结果列表。
Consider making a system call to the fping utility. Granted, it's not managed code, but fping is very well optimized for your specific task and would simplify your problem down to a single call followed by the processing of a text-based list of results.
您可以使用 powershell
,然后使用
resultat("ping -l 10 -n 2 " + your_ip);
you can use powershell
and then use
resultat("ping -l 10 -n 2 " + your_ip);