473,700 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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() { }
  5.     private void connect(object data)
  6.     {
  7.          Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  9.         try
  10.         {
  11.             socket.Connect((string)data, 21);
  12.         }
  13.         catch { }
  14.         finally
  15.         {
  16.             socket.Close();
  17.         }
  18.     }
  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.         }
  29.         for (int i = 0; i < 255; i++)
  30.         {
  31.             threads[i].Join();
  32.         }
  33. }
I use that class as
Expand|Select|Wrap|Line Numbers
  1.         IPScanner ip_scanner = new IPScanner();
  2.         ip_scanner.Scan();
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() { }
  5.     private void connect(object data)
  6.     {
  7.          Sleep(10000);
  8.          // Or do some math.
  9.     }
  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.         }
  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();
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 1202

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

Similar topics

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 state and decides if things are "good" or "bad". The second thread processes data, and the processing depends on the "good" or "bad" state at the time the data is processed. Sort of like this:
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 machine/IP address, then i need to make another socket and execute task." My query is. Is this possible that my main thread and my newly born thread
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 it seems to stay in the application scope, but the paranoid side of me thinks I may be missing something... any confirmation would be helpful.
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 connection will be accepted. then messages will be received and passed to the main thread via a delegate or event for processing. Messages will also be sent in return to the other application in an asynchronous manner. Questions: 1) from what it...
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 since I'm asking in the first place, assume that assumption to be very assuming. thanks, Eric
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 thru Try If mUIO_Threads(i) Is Nothing Then mUIO_Threads(i) = New System.Threading.Thread(AddressOf mUIO_DAQ(i).InitDAQ) mUIO_Threads(i).Name = mUIO_DAQ(i).UIO_IPAddr mUIO_Threads(i).IsBackground = False
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 to My first step was to use TcpClient, but there are nothing to control Timeout Connection... and I wasn't interested to wait 25-30sec for each ip. After some search, it's look like Socket was the solution with
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 'Invalid argument' exception. Anyone knows why? I'm using windows xp. my source code is here: http://pastebin.com/m23e633a2
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. read_socket calls connect on the socket it is passed. write_socket calls accept on the socket it is passed. So a single socket has connect and accept called on it. Now, main does call create_socket twice, so this does happen to two sockets, but it's...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.