Reputation: 33
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.)
Upvotes: 1
Views: 408
Reputation: 28355
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 callJavaScript
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.
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:
Default performance constants do cover your limitations for up to 1000 simultaneous requests.
Upvotes: 2