0

I created very simple scanner that will take ip range, get random ip within this range and check if particular port is opened. I use TcpClient

string PORT_CHECK(string IPorNAME, int port)
{
    TcpClient tcpClient = new TcpClient();
    tcpClient.ReceiveTimeout = 10000;
    tcpClient.SendTimeout = 10000;
    try
    {
        tcpClient.Connect(IPorNAME, port);
        return IPorNAME + " : OPEN";
    }
    catch (Exception)
    {
        return IPorNAME + " : CLOSED";
    }
}

I use threadpool

void main()
{
    while (stopper == false)
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback(bGround));
        Thread.Sleep(1);
    }
}

It checks about 10 ips per second, for example a scanner like VNC achieves a speed of about 500 per second, where this major difference comes from, and what to do to speed up my code?

Prime
  • 2,410
  • 1
  • 20
  • 35
  • 1
    You've set `ReceiveTimeout` and `SendTimeout` both to `10000`ms (10 seconds). Which means it will wait 10 seconds until it decides it's not there, I'd suggest lowering this number greatly, somewhere around one second (`1000`) or lower. – Prime Jun 02 '14 at 03:47
  • @AlphaDelta - you should promote this as an answer. – STLDev Jun 02 '14 at 03:49
  • @STLDeveloper There we go! – Prime Jun 02 '14 at 03:50
  • I have tried 1000, also 3000, it is slightly faster but I get less IPs with open ports (I mean really slightly faster) – user3192953 Jun 02 '14 at 03:52
  • I don't get your threadpool queue, what is `bGround`? – jgauffin Jun 02 '14 at 05:28

1 Answers1

2

You've set ReceiveTimeout and SendTimeout both to 10000ms (10 seconds). Which means it will wait 10 seconds until it decides it's not there, I'd suggest lowering this number greatly, somewhere around one second (1000) or lower.

Prime
  • 2,410
  • 1
  • 20
  • 35
  • No, It's not that, I tried with 1000 ms and with 100 ms and I get the same results... – user3192953 Jun 02 '14 at 04:09
  • 1
    I don't get it. How the send/receive timeouts could affect this ? Here the socket is simply connected, nothing is being sent or received. – quantdev Jun 02 '14 at 04:18