Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7739391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:30:39+00:00 2026-06-01T08:30:39+00:00

You may skip this part: I am creating an application where the client needs

  • 0

You may skip this part:

I am creating an application where the client needs to find the server on the same network.

The server:

public static void StartListening(Int32 port)
{

 TcpListener server = new TcpListener(IP.GetCurrentIP(), port);
 server.Start();

 Thread t = new Thread(new ThreadStart(() =>
   {
     while (true)
       {
         // wait for connection
         TcpClient client = server.AcceptTcpClient();
         if (stopListening)
           {                        
             break;
           }
       }
   }));
 t.IsBackground = true;
 t.Start();

}

Let’s say the server is listening on port 12345

then the client:

  • get the current ip address of the client let’s say it is 192.168.5.88
  • create a list of all posible ip addresses. The server ip address will probably be related to the client’s ip if they are on the same local network therefore I construct the list as:

       192.168.5.0
       192.168.5.1
       192.168.5.2
       192.168.5.3
       .....etc
       .....
       192.168.0.88
       192.168.1.88
       192.168.2.88
       192.168.3.88
       ...etc
       192.0.5.88 
       192.1.5.88
       192.2.5.88
       192.3.5.88
       192.4.5.88
       ..... etc
       0.168.5.88
       1.168.5.88
       2.168.5.88
       3.168.5.88
       4.168.5.88
       .... etc       
    

Then I try to connect with every possible ip and port 12345. If one connection is successful then that means that I found the address of the server.


Now my question is:

Now I have done this in two ways. I know just the basics about threads and I don’t know if this is dangerous but it works really fast.

        // first way
        foreach (var ip in ListOfIps)
        {               
            new Thread(new ThreadStart(() =>
            {
                TryConnect(ip);
            })).Start();
        }

the second way I belive it is more safe but it takes much more time:

        // second way
        foreach (var ip in ListOfIps)
        {               
            ThreadPool.QueueUserWorkItem(new WaitCallback(TryConnect), ip);
        }

I have to call the TryConnect method about 1000 times and each time it takes about 2 seconds (I set the connection timeout to 2 seconds). What will be the most efficient and secure way of calling it 1000 times?


EDIT 2

Here are the results using different techniques:

1) Using threadpool

        ..
        ..
        var now = DateTime.Now;
        foreach (var item in allIps)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork), item);
        }

        ThreadPool.QueueUserWorkItem(new WaitCallback(PrintTimeDifference), now);
    }

    static void PrintTimeDifference(object startTime)
    {
        Console.WriteLine("------------------Done!----------------------");
        var s = (DateTime)startTime;
        Console.WriteLine((DateTime.Now-s).Seconds);
    }

enter image description here

It took 37 seconds to complete

2) Using threads:

        ..
        ..
        var now = DateTime.Now;
        foreach (var item in allIps)
        {
            new Thread(new ThreadStart(() =>
            {
                DoWork(item);

            })).Start();

        }

        ThreadPool.QueueUserWorkItem(new WaitCallback(PrintTimeDifference), now);

enter image description here

It took 12 seconds to complete

3) Using tasks:

        ..
        ..
        var now = DateTime.Now;
        foreach (var item in allIps)
        {

            var t = Task.Factory.StartNew(() =>

                DoWork(item)
            );                                
        }

        ThreadPool.QueueUserWorkItem(new WaitCallback(PrintTimeDifference), now);
    }

    static void PrintTimeDifference(object startTime)
    {
        Console.WriteLine("------------------Done!----------------------");
        var s = (DateTime)startTime;
        Console.WriteLine((DateTime.Now-s).Seconds);
    }

enter image description here

It took 8 seconds!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:30:41+00:00Added an answer on June 1, 2026 at 8:30 am

    In this case I would prefer the solution with the ThreadPool-Threads, because creating 1000 Threads is a heavy operation (when you think of the memory each thread gets).
    But since .NET 4 there is another solution with the class Task.
    Tasks are workloads which can be executed in parallel. You can define and run them like this:

    var t = Task.Factory.StartNew(() => DoAction());
    

    You don’t have to care about the number of threads used because the runtime environment handles that. So if you have the possibility to split your workload into smaller packages which can be executed in parallel I would use Tasks to do the work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be duplicate question but I could not find one on SO. If
I know this may be a silly question but for this application I cannot
May be I am getting old, but I can't find it...
may i know how to use this getMicrophone with simpleFLVWriter to create flv with
May I know what is the workaround to make this code passed, in Visual
I was asked this in an interview, If you to find out if a
Edit: This may be a 5.0-specific bug. (I'm on 5.0.83). Removing the innodb_log_file_size setting
This question may me seems out of bound but its needed in my case
This may at first seem like an odd question, but when a cin request
This seems true but I can't find anyone on the internet saying it is,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.