473,624 Members | 2,269 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() { }
  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 1196

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

Similar topics

2
1456
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:
1
1167
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
5
4221
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.
1
1214
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...
9
1871
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
2
2347
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 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
2
4537
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 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
0
2212
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
0
2949
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...
0
8240
marktang
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...
0
8175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8625
jinu1996
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...
1
8336
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,...
1
6111
isladogs
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...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1487
bsmnconsultancy
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.