C# 正在获取IP超时ping回复
我正在ping一个域名列表(1000-10000)并让我的应用计算有多少是在线的,有多少是离线的。我遇到的问题是,我想列出超时机器的IP地址。有办法吗C# 正在获取IP超时ping回复,c#,visual-studio,asynchronous,ping,C#,Visual Studio,Asynchronous,Ping,我正在ping一个域名列表(1000-10000)并让我的应用计算有多少是在线的,有多少是离线的。我遇到的问题是,我想列出超时机器的IP地址。有办法吗 public async void PingCompletedMethod(object sender, PingCompletedEventArgs e) { // Sorts results of ping process PingReply rep = e.Reply;
public async void PingCompletedMethod(object sender, PingCompletedEventArgs e)
{
// Sorts results of ping process
PingReply rep = e.Reply;
try
{
if (e.Error != null)
{
// Counts number of unreachable endpoints
errorPings++;
ErrorPing.Text = Convert.ToString(errorPings);
ErrorPing.Background = Brushes.OrangeRed;
}
else
{
// Counts Pings that were succesfull
successPings++;
SuccessPing.Text = Convert.ToString(successPings);
}
// Counts Online/Offline responses
if (rep.Status == IPStatus.Success)
{
regOnline++;
onlineResult.Text = Convert.ToString(regOnline) + " Endpoints Online";
onlineResult.Background = Brushes.LightGreen;
}
else if (rep.Status == IPStatus.TimedOut)
{
// exportList.Add(Convert.ToString(rep.Address));
regOffline++;
offlineResult.Text = Convert.ToString(regOffline) + " Endpoints Offline";
offlineResult.Background = Brushes.Orange;
}
}
catch (Exception ex)
{
regOffline++;
}
当我尝试在TimedOut循环下获取rep.Address时,每个条目都会得到0.0.0.0。如果成功循环中添加了相同的内容,我将获得IP。我想获得TimedOut Ping的IP或域名
下面是ping:
foreach (string regOne in regOneList)
{
System.Threading.AutoResetEvent autores = new System.Threading.AutoResetEvent(false);
Ping pingasync = new Ping();
pingasync.PingCompleted += new PingCompletedEventHandler(PingCompletedMethod);
pingasync.SendAsync(regOne, 1000);
autores.WaitOne(1);
}
提前感谢Peter。您可以将域名作为用户状态传递,如下所示:
pingasync.SendAsync(regOne, 1000, regOne);
var reg = (string) e.UserState;
然后在PingCompletedMethod
中,您可以得到如下结果:
pingasync.SendAsync(regOne, 1000, regOne);
var reg = (string) e.UserState;
旁注:似乎您认为您正在使用以下代码通过超时:
pingasync.SendAsync(regOne, 1000);
但实际上,您正在将数字“1000”作为用户状态传递。要通过超时,您需要调用:
pingasync.SendAsync(regOne, 1000, null);
默认超时时间为5秒,因此您当前的代码将以该超时时间ping,而不是像您预期的那样以1秒时间ping。您可以将域名作为用户状态传递,如下所示:
pingasync.SendAsync(regOne, 1000, regOne);
var reg = (string) e.UserState;
然后在PingCompletedMethod
中,您可以得到如下结果:
pingasync.SendAsync(regOne, 1000, regOne);
var reg = (string) e.UserState;
旁注:似乎您认为您正在使用以下代码通过超时:
pingasync.SendAsync(regOne, 1000);
但实际上,您正在将数字“1000”作为用户状态传递。要通过超时,您需要调用:
pingasync.SendAsync(regOne, 1000, null);
默认超时时间为5秒,因此您当前的代码将以该超时时间ping,而不是像您预期的那样以1秒时间ping。非常感谢您的帮助(同时感谢超时提示)!我这样使用它:
exportList.Add(Convert.ToString(e.UserState);
非常感谢您的帮助(也感谢超时提示)!我这样使用它:exportList.Add(Convert.ToString(e.UserState);