Parallel.For(from, to, (port) => {});
class Program
{
static void Main(string[] args)
{
if (args.Length < 2 || args.Length > 3)
throw new ArgumentException();
var to = 0;
var from = 0;
var host = args[0];
#region argument validation
//...
#endregion
Console.WriteLine("Scanning {0} ports from: {1} to {2}", host, from, to - 1);
// parallel port scan
Parallel.For(from, to, (port) =>
{
var tcpScan = new TcpClient();
try
{
tcpScan.Connect(host, port);
if (tcpScan.Connected)
Console.WriteLine("Listening on port: {0}", port);
}
catch (Exception)
{
}
finally
{
if (tcpScan.Connected)
tcpScan.Close();
}
});
Console.ReadKey();
}
}