Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Multiple Server Instances with SuperSocket 2.0

Tech May 16 2

Configuring Multiple SuperSocket Servers

This implementation demonstrates how to configure and run multiple SuperSocket server instances within a single .NET Core application process. Each server instance operates independently with its own configuration, session handling, and command set.

Project Setup and Dependencies

Create a .NET Core 3.1 console application and install the SuperSocket 2.0.0-beta.8 package via NuGet. Add an appsettings.json file to the project root and set its copy property to "Copy if newer".

Server Configurasion

Define multiple server configurations in the appsettings.json file:

{
  "serverOptions": {
    "TelnetServerA": {
      "name": "TelnetServiceA",
      "listeners": [
        {
          "ip": "Any",
          "port": "4040"
        }
      ]
    },
    "TelnetServerB": {
      "name": "TelnetServiceB",
      "listeners": [
        {
          "ip": "Any",
          "port": "4041"
        }
      ]
    }
  }
}

Implementation Code

The following code demonstrates how to configure and run multiple server instances using SuperSocket's MultipleServerHostBuilder:

using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SuperSocket;
using SuperSocket.Command;
using SuperSocket.ProtoBase;
using SuperSocket.Server;

namespace MultiServerDemo
{
    class Application
    {
        static async Task Execute()
        {
            var hostBuilder = MultipleServerHostBuilder.Create()
                .AddServer<TelnetServiceA<StringPackageInfo>, StringPackageInfo, CommandLinePipelineFilter>(configurator =>
                {
                    configurator.ConfigureServerOptions((context, configuration) =>
                        {
                            return configuration.GetSection("TelnetServerA");
                        })
                        .UseSession<TelnetSessionA>()
                        .UseCommand(options =>
                        {
                            options.AddCommand<AdditionCommand>();
                            options.AddCommand<DivisionCommand>();
                            options.AddCommand<EchoCommand>();
                        });
                })
                .AddServer<TelnetServiceB<StringPackageInfo>, StringPackageInfo, CommandLinePipelineFilter>(configurator =>
                {
                    configurator.ConfigureServerOptions((context, configuration) =>
                        {
                            return configuration.GetSection("TelnetServerB");
                        })
                        .UseSession<TelnetSessionB>()
                        .UseCommand(options =>
                        {
                            options.AddCommand<MultiplicationCommand>();
                            options.AddCommand<SubtractionCommand>();
                            options.AddCommand<EchoCommand>();
                        });
                })
                .ConfigureLogging((context, logging) =>
                {
                    logging.AddConsole();
                })
                .Build();

            try
            {
                await hostBuilder.RunAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Server execution failed: {ex.Message}");
            }
        }
    }
}

Each server instance runs as a hosted service within the same process. For multi-process or distributed deployment scenarios, consider using container technologies like Docker.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.