none
How to Concurrently accept max number of TCP Sockets in C# RRS feed

  • Question

  • Hi All,

    I have to handle(accept)  5000+ sockets has to be accepted at the same time (or with minimum delay).

    So Sync or Async socket accept which is the best option ?

    And Please suggest best option to accept multiple tcp sockets at a same time(concurrently) using a tcp server listener

    Also processing the all sockets concurrently using thread

    Thanks,

    Gowtham




    Wednesday, November 18, 2015 6:50 AM

Answers

  • Hi Gowtham,

    >>So Sync or Async socket accept which is the best option ?

    I suggest you using async.

    >>And Please suggest best option to accept multiple tcp sockets at a same time(concurrently) using a tcp server listener

    I suggest you using ThreadPool to manage thread for you. Code below is for your reference.

    class Program
    {
        public async static void Main()
        {
            TcpListener listener = new TcpListener(IPAddress.Loopback, 12345);
            listener.Start();
            while (true)
            {
                TcpClient client = await listener.AcceptTcpClientAsync();
                ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessRequest), client);
            }
        }
    
        private static void ProcessRequest(object client)
        {
            //Handler Request
        }
    }
    Best Regards,
    Jerry
    Thursday, November 19, 2015 1:45 AM