在我的程序中,我将扫描(ping)网络中的所有地址,从x.x.x.1到x.x.x.255。我在cmd中测试了这个命令,它运行正常,但无法从程序运行。
我设置程序允许创建窗口并在命令结束后保持打开状态。该过程确实工作了一段时间,它从x.x.x.1 ping到x.x.x.18然后它卡住了。我尝试手动ping x.x.x.18以查看该地址的特殊之处......但没有什么特别的,只是一个与x.x.x.2到x.x.x.17相同的未占用地址。
任何人都可以诊断出这个问题吗?谢谢!
public void PingAllAddressInVirtualNetwork () {
string hotspotAddress = GetHotspotDefaultAddress ();
string cmd = "/k \"for /l %i in (1,1,255) do ping " + hotspotAddress.Substring (0, hotspotAddress.Length - 1) + "%i -n 1 -w 15\"";
UnityEngine.Debug.Log ("cmd = "+cmd);
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe", cmd);
processStartInfo.RedirectStandardOutput = true;
//processStartInfo.CreateNoWindow = true;
//processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.UseShellExecute = false;
processStartInfo.StandardOutputEncoding = Encoding.Default;
Process process = Process.Start (processStartInfo);
if (process != null) {
process.WaitForExit ();
UnityEngine.Debug.Log ("ping address (scanning) finished.");
}
}
答案 0 :(得分:0)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
class IPPing
{
/// <summary>
/// Ping List
/// </summary>
private static List<Ping> m_PingList = new List<Ping>();
/// <summary>
/// used like event Handler count
/// </summary>
private static int m_nInstance = 0;
/// <summary>
/// Lock
/// </summary>
private static object @lock = new object();
/// <summary>
/// IpAddress
/// </summary>
private static IPAddress IpAddress { get; set; }
/// <summary>
/// Number of ip to check
/// </summary>
private static int NumberOfIp { get; set; }
/// <summary>
/// IpList
/// </summary>
private static List<IPAddress> IpList { get; set; }
/// <summary>
/// Constructor
/// </summary>
/// <param name="nNumberOfIp">Number of ip to check</param>
/// <param name="StartIp">Start ip</param>
public IPPing(int nNumberOfIp, IPAddress StartIp)
{
NumberOfIp = nNumberOfIp;
IpAddress = StartIp;
IpList = new List<IPAddress>();
}
public List<IPAddress> StartPing()
{
// Create ping request
CreatePingers(NumberOfIp);
PingOptions pingOption = new PingOptions(1, true);
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes("");
// Used to wait thread for ping request to finished
SpinWait wait = new SpinWait();
// Send all ping Async
foreach (Ping ping in m_PingList)
{
lock (@lock)
{
m_nInstance += 1;
}
string sasd = IpAddress.ToString();
ping.SendAsync(IpAddress.ToString(), 250, data, pingOption);
IpAddress = GetNextIp(IpAddress);
}
// wait still all Pin_Completed finished
while (m_nInstance > 0)
{
wait.SpinOnce();
}
DestroyPingers();
return IpList;
}
/// <summary>
/// Event is called when ping reqest finished
/// </summary>
/// <param name="obj">object</param>
/// <param name="e">Ping Completed Event Args</param>
public static void Ping_completed(object obj, PingCompletedEventArgs e)
{
lock (@lock)
{
m_nInstance -= 1;
}
if (e.Reply.Status == IPStatus.Success)
{
IpList.Add(e.Reply.Address);
}
}
/// <summary>
/// Used to create ping request
/// </summary>
/// <param name="nNumberOfPingRequest">Number of ping request</param>
public static void CreatePingers(int nNumberOfPingRequest)
{
for (int i = 1; i <= nNumberOfPingRequest; i++)
{
Ping ping = new Ping();
// Attached event to ping request
ping.PingCompleted += Ping_completed;
m_PingList.Add(ping);
}
}
/// <summary>
/// Used to destroy ping requestes
/// </summary>
public static void DestroyPingers()
{
foreach (Ping ping in m_PingList)
{
// Detach event
ping.PingCompleted -= Ping_completed;
ping.Dispose();
}
m_PingList.Clear();
}
/// <summary>
/// Get next ip
/// </summary>
/// <param name="Ip">Ip address</param>
/// <returns>Next ip</returns>
public static IPAddress GetNextIp(IPAddress Ip)
{
byte[] barrIP = Ip.ToString().Split('.').Select(byte.Parse).ToArray();
if (++barrIP[3] == 0)
{
if (++barrIP[2] == 0)
{
if (++barrIP[1] == 0)
{
++barrIP[0];
}
}
}
return new IPAddress(barrIP);
}
}
这将帮助您ping所有IP并更快地ping ip。在这里你可以给你想要Ping的启动ip和ip数量
用法:
IPAddress IpAddress = IPAddress.Parse("199.199.199.0");
IPPing objIP = new IPPing(255,IpAddress);
List<IPAddress> IpList= objIP.StartPing();
这将从199.199.199.0开始ping下一个255 ip