none
Efficiently handle socket connection in C# RRS feed

  • Question

  • I currently have a 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);
        // Create a new thread which handles the specific socket for future requests (TCP read or write operation)
        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?


    Thanks, Gowtham


    Tuesday, October 24, 2017 7:55 PM

All replies

  • Hi Gowtham,

    For async operation, you may consider Task instead of BeginAcceptTcpClient. But, you may consider whether you have such computer resource to open so many connections.

    I think you could set up multiple TCP Server to receive client sockets and then use Load Balancing to router the Socket requests.

    Best Regards,

    Edward


    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 2:00 AM
  • How i can use my cloud Server ip instead of local ip....

    and i need to send data using that ip and port

    var socketAccept = new TcpListener(IPAddress.Any, 1234);

    Tuesday, February 19, 2019 1:01 PM