By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,750 Members | 1,456 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,750 IT Pros & Developers. It's quick & easy.

C# WEB/APP: Joining threads that execute Socket.Connect

P: 1
I write ip/port scanner so users using search on our LAN know if the particular ftp is online or not.

The class I write has the following structure:

Expand|Select|Wrap|Line Numbers
  1. public class IPScanner
  2. {
  3.     public IPScanner() { }
  4.  
  5.     private void connect(object data)
  6.     {
  7.          Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  8.  
  9.         try
  10.         {
  11.             socket.Connect((string)data, 21);
  12.         }
  13.         catch { }
  14.         finally
  15.         {
  16.             socket.Close();
  17.         }
  18.     }
  19.  
  20.     public void Scan()
  21.     {
  22.         Thread[] threads = new Thread[255];
  23.         for (int i = 0; i < 255; i++)
  24.         {
  25.             threads[i] = new Thread(new ParameterizedThreadStart(connect));
  26.             threads[i].Start( "xxx.xxx.xxx." + ((int)(i+1)).ToString() );
  27.         }
  28.  
  29.         for (int i = 0; i < 255; i++)
  30.         {
  31.             threads[i].Join();
  32.         }
  33. }
  34.  
I use that class as
Expand|Select|Wrap|Line Numbers
  1.         IPScanner ip_scanner = new IPScanner();
  2.         ip_scanner.Scan();
  3.  
But Joins work synchronously in that sense that the next Join in the for-loop executes only after previous one returns. So the scan of one address range consumes very much time. I test it using only 4 adresses I sure are offline and trying to Join to 5 threads. I receive the null reference error only after 1-2 minutes of executing of program.

But the following code works fine and returns in 10 seconds (this code is met often in google but applied not to sockets):
Expand|Select|Wrap|Line Numbers
  1. public class IPScanner
  2. {
  3.     public IPScanner() { }
  4.  
  5.     private void connect(object data)
  6.     {
  7.          Sleep(10000);
  8.          // Or do some math.
  9.     }
  10.  
  11.     public void Scan()
  12.     {
  13.         Thread[] threads = new Thread[255];
  14.         for (int i = 0; i < 255; i++)
  15.         {
  16.             threads[i] = new Thread(new ParameterizedThreadStart(connect));
  17.             threads[i].Start( "xxx.xxx.xxx." + ((int)(i+1)).ToString() );
  18.         }
  19.  
  20.         for (int i = 0; i < 255; i++)
  21.         {
  22.             threads[i].Join();
  23.         }
  24. }
  25. ...
  26.         IPScanner ip_scanner = new IPScanner();
  27.         ip_scanner.Scan();
  28.  
What is the problem with sockets? I use asp.net2 and IIS 7 on Vista 64. (I tried the same in winform application with the same results.)
Thank you.
Dec 22 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.