public static void Work(IpRange IpRange, PortRange PortRange, int timeOut) { DateTime start = DateTime.Now; Console.WriteLine("Starting..."); Parallel.ForEach(IpRange, new ParallelOptions { MaxDegreeOfParallelism = 50 }, (ip) => { foreach (int port in PortRange) { Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp); var result = socket.BeginConnect(ip, port, null, null); bool success = result.AsyncWaitHandle.WaitOne(timeOut); if (success) { Console.WriteLine($"Found port open {port} @ {ip.MapToIPv4().ToString()}"); } else { socket.Close(); } } }); DateTime end = DateTime.Now; Console.WriteLine($"Done in {end - start}"); }
private static void Run(Options options) { //Checking input errors try { IpRange = IpRange.Parse(options.IpRange); PortRange = PortRange.Parse(options.Ports); } catch (Exception e) { Console.WriteLine(e.Message); } Worker.Work(IpRange, PortRange, options.timeOut); }