我正在尝试将IP设备连接到我的网络(我所连接的WLAN)。首先,我在Win8中使用命令行并在知道我自己的IP(每次递增和ping)时连续ping通。要以编程方式执行此操作,请执行WnetWatcher我通过调用传递attempts=4
和timeout=3
的函数来使用Ping Class,但是显示PROCESS_HAS_BLOCKED_PAGES
和{{3}的蓝屏它是一个潜在的API问题。任何人都有比这更好的想法来获取所有设备的IP,因为SO的几个线程使用Dns
类找到它但是适用于单个PC(我的)。
1)。什么是Ping
的备用,如果它是Ping
,那么如何解决API问题。
2)。另外,我如何获得路由器IP,以便我可以在网络上运行其他IP的环路,或者有更好的替代方案吗?
public static void Ping(string host,int attempts,int timeout) {
new Thread(delegate()
{
try
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
ping.PingCompleted += new PingCompletedEventHandler(PingCompleted);
ping.SendAsync(host, timeout, host);
}
catch
{
// Do nothing and let it try again until the attempts are exausted.
// Exceptions are thrown for normal ping failurs like address lookup
// failed. For this reason we are supressing errors.
}
}).Start();
}
private static void PingCompleted(object sender, PingCompletedEventArgs e)
{
string ip = (string)e.UserState;
if (e.Reply != null && e.Reply.Status == IPStatus.Success)
{
// Logic for Ping Reply Success
// Console.WriteLine(String.Format("Host: {0} ping successful", ip));
lstlocal.listViewItem //Error ...the intellisense is not accepting it here
}
else
{
// Logic for Ping Reply other than Success
}
}
//function caller code from a button
lstLocal.Items.Clear();
lstLocal.FullRowSelect = true;
bool value;
for (int i = 0; i <= 254; i++)
{
string ping_var = "192.168.1" + "." + i;
value = Ping(ping_var, 4, 3);
// MessageBox.Show("Ping response for"+ping_var +"is" + value);
if(value==true)
{
ListViewItem items=new ListViewItem(ping_var.ToString());
lstLocal.Items.Add(items);
}
}
}
答案 0 :(得分:1)
1)您可以调用Ping类的SendAsync方法而不是Send来避免阻塞:
public void Ping(string host, int attempts, int timeout)
{
for (int i = 0; i < attempts; i++)
{
new Thread(delegate()
{
try
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
ping.PingCompleted += new PingCompletedEventHandler(PingCompleted);
ping.SendAsync(host, timeout, host);
}
catch
{
// Do nothing and let it try again until the attempts are exausted.
// Exceptions are thrown for normal ping failurs like address lookup
// failed. For this reason we are supressing errors.
}
}).Start();
}
}
并在PingCompleted EventHandler委托中处理ping响应:
private void PingCompleted(object sender, PingCompletedEventArgs e)
{
string ip = (string)e.UserState;
if (e.Reply != null && e.Reply.Status == IPStatus.Success)
{
// Logic for Ping Reply Success
ListViewItem item = new ListViewItem(ip);
if (this.InvokeRequired)
{
this.Invoke(new Action(() =>
{
lstLocal.Items.Add(item);
}));
}
else
{
lstLocal.Items.Add(item);
}
// Logic for Ping Reply Success
// Console.WriteLine(String.Format("Host: {0} ping successful", ip));
}
else
{
// Logic for Ping Reply other than Success
}
}
2)获取路由器IP或网关:
static string NetworkGateway()
{
string ip = null;
foreach (NetworkInterface f in NetworkInterface.GetAllNetworkInterfaces())
{
if (f.OperationalStatus == OperationalStatus.Up)
{
foreach (GatewayIPAddressInformation d in f.GetIPProperties().GatewayAddresses)
{
ip = d.Address.ToString();
}
}
}
Console.WriteLine(string.Format("Network Gateway: {0}", ip));
return ip;
}