473,388 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 1166

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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
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,...
0
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...
0
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...

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.