Implementing Multiple Server Instances with SuperSocket 2.0
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.