Skip to content

Port efficiency #2

@drasco

Description

@drasco

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions