C-Sharp-Multi-Threaded-Port-Scanner icon indicating copy to clipboard operation
C-Sharp-Multi-Threaded-Port-Scanner copied to clipboard

Port efficiency

Open drasco opened this issue 3 years ago • 0 comments

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;
     }
    

drasco avatar Oct 11 '22 03:10 drasco