简体   繁体   中英

Sockets in .NET - Getting started with a multithreaded server

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.)

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.

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM