0

I am trying to create port scanner in C#, so far I have this:

private void ip()
{
    int start = 70;
    int end = 80;
    string ipString;
    if (this.comboBox1.Text == "Domain") {
         ipString= Dns.GetHostEntry(txtHost.Text).AddressList[0].ToString();
    }
    else {
        ipString = txtHost.Text;
    }
    TcpClient asd = new TcpClient();
    // MessageBox.Show(ipString);     
    IPAddress address = IPAddress.Parse(ipString);
    for (int i = start; i <= end; i++)
    {
        try
        {
            asd.SendTimeout = 3000;
            asd.ReceiveTimeout = 3000;
            asd.Connect(address, i);

            if (asd.Connected)
            {
                MessageBox.Show("Port " + i + " is open");
            }
        }
        catch
        {
            MessageBox.Show("Port " + i + " is closed");
        }
    }
}

However for closed ports, it's kinda slow, around 20 seconds, what should I do to make the process faster? Thanks a lot.

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
user1815324
  • 269
  • 1
  • 4
  • 10
  • 1
    "it's kinda slow". You should measure which bit is slow so we don't have to guess. – spender Nov 25 '12 at 14:03
  • Maybe related: http://stackoverflow.com/questions/1331902/c-sharp-tcp-port-scanner-resources – rene Nov 25 '12 at 14:04
  • You should consider the async networking APIs. There are many to choose from, but if you value your sanity, consider .net4.5 and the async/await apis. Also worth considering that none of the .net DNS apis are any good. Perhaps a 3rd party option might be considered. See: http://stackoverflow.com/questions/11480742/dns-begingethost-methods-blocking – spender Nov 25 '12 at 14:07
  • Slow, around 20 seconds. – user1815324 Nov 25 '12 at 14:12

3 Answers3

1

You should use threading.

http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx

Fire up separate thread for every port you want to check and let the callbacks inform you which of them is open and which is not.

Igor Perić
  • 171
  • 1
  • 8
  • 1
    How can I call thread with multiple parameters ? private void ip(strin ip, int start, int end) { } ? – user1815324 Nov 25 '12 at 14:04
  • 3
    How can you suggest this when you don't even know which part is "kinda slow"? Adding extra threads is rarely a good answer to speeding up networking code. – spender Nov 25 '12 at 14:05
  • 1
    Threading is going to be needed in any case since his code would block the main UI thread no matter what he did. Networking code needs threading. – Igor Perić Nov 25 '12 at 14:17
  • @IgorPerić Not necessarily, there are quite capable async methods in TcpClient too. – Joachim Isaksson Nov 25 '12 at 15:51
0

You should have your delegate and async function declared like this (two parameters in example)

public delegate void UpdateUIThreadDelegate(int par1, int par2);
void UpdateUIThread(int par1, int par2)
{
       ...
}

And you should call you async function like this:

this.Invoke(new UpdateUIThreadDelegate(UpdateUIThread), new object[] { par1, par2 });
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Igor Perić
  • 171
  • 1
  • 8
0

You need to close the connection after you are done testing it.

try asd.Close(); at the end of your catch statement.

It sped up the process for me significantly.

Andre Silva
  • 4,782
  • 9
  • 52
  • 65