static void StartScanner(List <IPAddress> listOfIpAddresses, string nrOfH, string nrOfT) { int nrOfHosts = Int32.Parse(nrOfH); int nrOfThreads = Int32.Parse(nrOfT); List <IPAddress> list = listOfIpAddresses; if (nrOfHosts > list.Count) { nrOfHosts = list.Count; } MutexClass mutobj = new MutexClass(); mutobj._mutex = new Mutex(); for (int i = 0; i < nrOfHosts; i++) { int thread_i = i; List <IPAddress> l = list; int noh = nrOfHosts; int not = nrOfThreads; MutexClass m = mutobj; var hostThread = new Thread(() => HostThreadWorker(l, noh, not, thread_i, m)); hostThread.Start(); } }
private static void HostThreadWorker(List <IPAddress> list, int nrOfHosts, int nrOfThreads, int i, MutexClass m) { int totalNrOfHosts = list.Count; int counter = 0; IPAddress ipAddrScanned = new IPAddress(0); PortList portListing = new PortList(1, 600); Cde cdeobj = new Cde(); cdeobj._countdownEvent = new CountdownEvent(nrOfThreads); while ((i + counter) <= (totalNrOfHosts - 1)) { { ipAddrScanned = list[i + counter]; portListing = new PortList(1, 65535); cdeobj = new Cde(); cdeobj._countdownEvent = new CountdownEvent(nrOfThreads); m._mutex.WaitOne(); try { counter += nrOfHosts; } finally { m._mutex.ReleaseMutex(); } } string str = string.Concat("Machine ", ipAddrScanned, " is being scanned"); Console.WriteLine(str); str.WriteDebug(); for (int j = 0; j < nrOfThreads; j++) { int k = j; IPAddress ip = ipAddrScanned; PortList p = portListing; Cde cdevent = cdeobj; var portThread = new Thread(() => PortThreadWorker(ip, k, cdevent, p)); portThread.Start(); } cdeobj._countdownEvent.Wait(); } }