1

Networking in C# is something I am relatively new to and so I was wondering how to get started with it for a particular project.

Ultimately, I want to develop a server which can have as many as one thousand clients connected to it concurrently (TCP protocol).

I understand creating a new thread for each client would be potentially quite inefficient, especially with the memory overhead of each thread once passing 100 clients for example.

So simply, what I am asking is, could anyone suggest anywhere I could find out more about getting started with developing 'multithreaded' servers for many clients.

(Please add a comment if this question is too broad.)

VMAtm
  • 27,943
  • 17
  • 79
  • 125
Wapted
  • 33
  • 5
  • 2
    It is not merely a C# (.NET) API question by nature. See http://smallvoid.com/article/winnt-tcpip-max-limit.html for a good albeit a bit aged summary of aspects which might spoil your plans. Also, given that in Windows a lot of system resources are of native type ``HANDLE`` (they are waitable) in more or less obvious fashion, you can use a thread pool easily to manage multiple connections. The 1 thread per connection approach is not your only option. Also, you might want to look up what a ``completion port`` is good for, as that will give you fresh ideas how to do what you try to do. – BitTickler Apr 12 '17 at 09:00

1 Answers1

2

You may give a try for SignalR for sockets.

What is SignalR?
ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of adding real-time web functionality to applications. Real-time web functionality is the ability to have server code push content to connected clients instantly as it becomes available, rather than having the server wait for a client to request new data.
...
SignalR provides a simple API for creating server-to-client remote procedure calls (RPC) that call JavaScript functions in client browsers (and other client platforms) from server-side .NET code. SignalR also includes API for connection management (for instance, connect and disconnect events), and grouping connections.

enter image description here

To implement a server you need to derive from Hub class:

using System;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalRChat
{
    public class ChatHub : Hub
    {
        public void Send(string name, string message)
        {
            // Call the broadcastMessage method to update clients.
            Clients.All.broadcastMessage(name, message);
        }
    }
}

And from client side javascript is like this:

var chat = $.connection.chatHub;
chat.client.broadcastMessage = function (name, message) {
  // interact with server
}
$.connection.hub.start().done(function () {
    $('#sendmessage').click(function () {
        // Call the Send method on the hub. 
        chat.server.send($('#displayname').val(), $('#message').val());
        // Clear text box and reset focus for next comment. 
        $('#message').val('').focus();
    });
});

Supported Platforms for SignalR are:

  • Server side
    • Windows Server 2012
    • Windows Server 2008 r2
    • Windows 8
    • Windows 7
    • Windows Azure
  • Client side
    • Browsers
    • Microsoft Internet Explorer versions 8, 9, 10, and 11. Modern, Desktop, and Mobile versions are supported.
    • Mozilla Firefox: current version - 1, both Windows and Mac versions.
    • Google Chrome: current version - 1, both Windows and Mac versions.
    • Safari: current version - 1, both Mac and iOS versions.
    • Opera: current version - 1, Windows only.
    • Android browser
    • Windows Desktop and Silverlight Applications
    • Applications using .NET 4 are supported on Windows XP SP3 or later.
    • Applications using .NET Framework 4.5 are supported on Windows Vista or later.
    • Windows Store and Windows Phone 8 Applications

Default performance constants do cover your limitations for up to 1000 simultaneous requests.

VMAtm
  • 27,943
  • 17
  • 79
  • 125
  • Wasnt really what I was looking for, however, I might keep that in mind for future. Thanks. However, I have sorted everything now and no longer need an answer to my question :) – Wapted May 07 '17 at 20:21
  • Then you should answer your question by yourself to provide a solution to others – VMAtm May 07 '17 at 21:25