472,374 Members | 1,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

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

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
0 1096

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

Similar topics

2
by: Gregory Bond | last post by:
I've had a solid hunt through the (2.3) documentation but it seems silent on this issue. I have an problem that would naturally run as 2 threads: One monitors a bunch of asyncrhonous external...
1
by: Sandeep Arya | last post by:
Hello I am developing an application using PyQT. My application scenario is:: "At any instance one socket is open in my application. Now if user wants to execute some command on another...
5
by: Russell Warren | last post by:
Does anyone know the scope of the socket.setdefaulttimeout call? Is it a cross-process/system setting or does it stay local in the application in which it is called? I've been testing this and...
1
by: MR | last post by:
this is a question on how to design an TCP Listener application. background: There will be several threads that listen on several ports (one per port). when another app requests to connect, the...
9
by: Eric Sabine | last post by:
Can someone give me a practical example of why I would join threads? I am assuming that you would typically join a background thread with the UI thread and not a background to a background, but...
2
by: Stressed Out Developer | last post by:
We have an application that has a 200 count loop that does the following: ' Each time thru the loop we pass the next IP Address is a range (aka 192.168.4.50 thru 192.168.4.254) Try If...
2
by: jgbid | last post by:
Hi, I'm trying to build an IP Scanner inc c# for a specific port (80) and for specific IP Ranges. For example 24.36.148.1 to 24.36.148.255 My first step was to use TcpClient, but there are...
0
by: Leo Jay | last post by:
I'd like to read and write the same socket in different threads. one thread is only used to read from the socket, and the other is only used to write to the socket. But I always get a 10022...
0
by: Jean-Paul Calderone | last post by:
On Sat, 23 Aug 2008 02:25:17 +0800, Leo Jay <python.leojay@gmail.comwrote: No - it's just what I said. create_socket creates one socket and passes it to read_socket and write_socket. ...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.