C-Sharp-Multi-Threaded-Port-Scanner
C-Sharp-Multi-Threaded-Port-Scanner copied to clipboard
Port efficiency
Hi mate.
I was able to get 300-500 threads going (depends on the system - specify 500, .net may only open 300) I believe ports were not being cleaned up perhaps. Much less cpu - and a quicker finish.
In PortScanner.start
-
ThreadPool.SetMaxThreads(threadCounter, threadCounter); -
for (int i = 0; i < threadCounter; i++) { if (!portList.MorePorts()) return;
In RunScanTcp
-
Thread.Seep(100)` //quicker, more threads, than 1ms -
var con = Connect(host, port, tcpTimeout); if (con != null) { con.Close(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("TCP Port {0} is open ", port); }
In Connect
-
Instead of throw Exception
newClient.Close(); return null;
PortList
-
private Object threadLock = new Object(); -
public bool MorePorts() { lock (threadLock) return (stop - ports) >= 0; } public int NextPort() { lock (threadLock) { if (MorePorts()) { return ports++; } } return -1; }