The class I write has the following structure:
Expand|Select|Wrap|Line Numbers
- public class IPScanner
- {
- public IPScanner() { }
- private void connect(object data)
- {
- Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- try
- {
- socket.Connect((string)data, 21);
- }
- catch { }
- finally
- {
- socket.Close();
- }
- }
- public void Scan()
- {
- Thread[] threads = new Thread[255];
- for (int i = 0; i < 255; i++)
- {
- threads[i] = new Thread(new ParameterizedThreadStart(connect));
- threads[i].Start( "xxx.xxx.xxx." + ((int)(i+1)).ToString() );
- }
- for (int i = 0; i < 255; i++)
- {
- threads[i].Join();
- }
- }
Expand|Select|Wrap|Line Numbers
- IPScanner ip_scanner = new IPScanner();
- ip_scanner.Scan();
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
- public class IPScanner
- {
- public IPScanner() { }
- private void connect(object data)
- {
- Sleep(10000);
- // Or do some math.
- }
- public void Scan()
- {
- Thread[] threads = new Thread[255];
- for (int i = 0; i < 255; i++)
- {
- threads[i] = new Thread(new ParameterizedThreadStart(connect));
- threads[i].Start( "xxx.xxx.xxx." + ((int)(i+1)).ToString() );
- }
- for (int i = 0; i < 255; i++)
- {
- threads[i].Join();
- }
- }
- ...
- IPScanner ip_scanner = new IPScanner();
- ip_scanner.Scan();
Thank you.