I' m trying to do a simple tcp port scan using socket connect, and I m using a thread pool, but I don't get the output that I expect, the code for the thread pool is from here.
My code:
IPAddress dstIpAddress ;
IPAddress.TryParse("192.168.2.106", out dstIpAddress);
Action<IPAddress,int> tcpConnect = (( dstIp, destinationPort) =>
{
string result = "open";
try
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(dstIpAddress, destinationPort);
}
catch (Exception e)
{
result = "closed";
}
Console.WriteLine("TCP port {0} is {1}.", destinationPort, result);
});
using (var pool = new ThreadPool(10))
{
for (var i = 0; i < 50; i++)
{
pool.QueueTask(() => tcpConnect(dstIpAddress,i));
}
}