none
How to efficiently handle socket connection in C# RRS feed

  • Question

  • Hi All,

    I currently have scenario where more than 10000 socket client trying to connect to my C# socket server which resides in cloud VM,

    I have written my  socket server in the below way,

         var socketAccept = new TcpListener(IPAddress.Any, 1234);
         socketAccept.Start(1000);
         socketAccept.BeginAcceptTcpClient(OnAccept, socketAccept);
          private void OnAccept(IAsyncResult res)
            {
                TcpListener listener = (TcpListener)res.AsyncState;
                TcpClient receiveSocket = listener.EndAcceptTcpClient(res);
                // TCP read or write operation goes here
                listener.BeginAcceptTcpClient(OnAccept, listener);
            }


    And this approach is not capable of handling ~1000 socket connection concurrently,

    Is there any efficient approach to handle this scenario,

    Please suggest logic/architectural changes required here.


    Thanks, Gowtham


    Tuesday, October 24, 2017 5:37 PM

All replies

  • Hi Gowthamaprabhu S,

    Thank you for posting here.

    For your question, you could try to use thread or parallel to make it more efficient.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 25, 2017 7:56 AM
    Moderator
  • Note the parameter you pass to TcpListener.Start(), literally it does what you ask it for.

    When your handler works extremely fast that it almost "finish it's work and disconnects" as soon as it connects, the number of pending connection it'll accept will look like the number of concurrent connection it supports.

    l would probably just remove that restriction and let the cloud plan control how much connections the code can support, but if you're using plans that'll automatically scales up when CPU/memory usage reaches limit, you may still want to put a cap there.
    Thursday, October 26, 2017 3:48 AM
    Answerer