2017-08-22 7 views
0

In meinem Programm werde ich alle Adressen in meinem Netzwerk von x.x.x.1 bis x.x.x.255 scannen (pingen). Ich habe den Befehl in cmd getestet, was gut funktioniert, aber nicht vom Programm ausgeführt werden kann.Command Line for-Schleife pausiert während der Ausführung

Ich habe mein Programm erlaubt Fenster erstellen und hält es auch nach dem Ende des Befehls. Der Prozess funktioniert eine Weile, es pingt von x.x.x.1 nach x.x.x.18 und dann bleibt es hängen. Ich habe versucht, manuell ping x.x.x.18, um zu sehen, was speziell für diese Adresse ist ... noch nichts besonderes, nur eine unbelegte Adresse wie x.x.x.2 bis x.x.x.17.

Jeder kann das Problem diagnostizieren? Vielen Dank!

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."); 
    } 
} 
+3

keine Lösung für Ihr Problem aber: Warum don Verwenden Sie [System.Net.NetworkInformation.Ping] (https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping (v = vs.110) .aspx)? – Fildor

+0

In Bezug auf Ihr Problem: Können Sie versuchen, StandardError auch umzuleiten? Und vielleicht schauen, was ist der ReturnCode, aber ich bezweifle, dass es nützlich sein wird, wenn es nur blockiert ... – Fildor

Antwort

1
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); 
    } 
} 

Dies hilft Ihnen, alle IP-ping und wird ip schneller ping. Hier können Sie Start-IP und die Anzahl der IP geben Sie

Nutzungs pingen möchten:

IPAddress IpAddress = IPAddress.Parse("199.199.199.0"); 
IPPing objIP = new IPPing(255,IpAddress); 
List<IPAddress> IpList= objIP.StartPing(); 

dies ping nächsten 255 ip von 199.199.199.0

+0

"Ich habe Probleme mit meinem Windows." - "Verwenden Sie Linux." ... Lustig beiseite. Nicht wirklich eine Lösung für das vorliegende Problem. Ja, es wird wahrscheinlich tun, was die Aufgabe ist. Aber die Frage ist, warum die OP-Lösung ein irriges Verhalten zeigt, nicht wie es anders zu machen ist. Deshalb habe ich gefragt, warum er Ping nicht in einem _comment_ verwendet. – Fildor

+0

@Fildor als OP sagte "Der Prozess funktioniert eine Weile", so 'Ping' verwenden wird am besten schneller ping – Blue

+0

Ich stimme zu, aber wir wissen immer noch nicht, warum OP-Code" funktioniert nicht mehr "... – Fildor