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 6903405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:55:17+00:00 2026-05-27T07:55:17+00:00

I am in the process of updating an app that is going from supporting

  • 0

I am in the process of updating an app that is going from supporting a single connection to being able to support numerous connections. I have figured that using the Task Parallel Library may be the best way to go about this in terms of creating and managing the multiple sockets there will be. As such, I have begun to write some code and have come across a possible issue while debugging. It seems that some of my tasks that I create to set up the connections never complete and am wondering if I am utilizing TPL correctly or if it is something else. If I debug the code, it appears as if only one task in the collection actually contains a Socket. Any assistance is appreciated. Here is the code from a sandbox console application.

Main:

        static void Main(string[] args)
        {
            Console.WriteLine("Creating connections.....");
            var sockets = CreateListeners(5);

            Console.WriteLine("Socket Info:");

            foreach (var socket in sockets)
            {                
                if (socket.Result != null)
                {
                    var con = socket.Result;
                    IPEndPoint ipep = (IPEndPoint)con.LocalEndPoint;
                    string port = ipep.Port.ToString();
                    Console.WriteLine("Socket #{0} - Listening on Port {1}:", socket.Id.ToString(), port);
                }
            }
            Console.WriteLine("Press any key to exit..");
            Console.ReadLine();
        }

Function that creates a list of Tasks:

private static List<Task<Socket>> CreateListeners(int numberToCreate)
            {
                var sockets = new List<Task<Socket>>();
                for (int n = 0; n < numberToCreate; n++)
                {
                    var currentSocket = Task<Socket>.Factory.StartNew(() => CreateSocketConnection(n));
                    sockets.Add(currentSocket);
                }

                return sockets;            
            }

Function that creates a single socket connection..

private static Socket CreateSocketConnection(int port)
            {

                try 
                {
                    IPAddress ipAddress = null;
                    IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
                    if (Socket.OSSupportsIPv6)
                    {
                        ipAddress = ipHostInfo.AddressList[1];
                    }
                    else
                    {
                        ipAddress = ipHostInfo.AddressList[0];
                    }


                    //Create a new connection on specified port
                    IPEndPoint endPoint = new IPEndPoint(ipAddress, port+6000);
                    Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    listener.Bind(endPoint);
                    //listener.Blocking = false;
                    listener.Listen(500);

                    return listener;

                }
                catch (Exception ex)
                {
                   throw ex;        
                }
                finally
                {

                }

                return null;            
            }
  • 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-05-27T07:55:18+00:00Added an answer on May 27, 2026 at 7:55 am

    Your problem seems to be that you’re launching async tasks but not waiting for them to complete before you query their result.

    After your CreateListeners call, you know that you’ve started 5 tasks. You let the task library decide about the parallelism or sequence.

    Therefore, you need to call socket.Wait(); before if (socket.Result != null), because it very well may be null if it hasn’t completed yet.

    As VirtualBlackFox has stated, it may very well be executing them sequentially if deems appropriate, which could mean that when you start querying results, only one task has completed yet.

    As a general answer, it doesn’t seem overkill to me to use parallism for this operation. IO-type operations can generally benefit a lot from parallism since they involve substantial overhead.

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

Sidebar

Related Questions

Suppose I have a process that is updating a record and encounters a record
I have implemented an app that is basically a custom app store for updating
I have an app which calls another process in a command window and that
I have a fairly simple console app that monitors an exchange mailbox, picks particular
I have recently updated Xcode to use the iOS5 SDK. Since updating my app
I am in the process updating my app so I use the Devise gem
I have an app with callbacks for recording and playing audio. From the recording
I have a MySQL 5 database that is updated every 5 minutes from a
I'm in the process of updating a framework that I wrote a while ago.
I have two distinct repositories that I will be using simultaneously throughout my app.

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.