Building a Telnet Server with SuperSocket 2.0

This tutorial demonstrates creating a basic Telnet server using SuperSocket 2.0 for handling simple arithmetic commands.

Project Setup

Create a new .NET Core 3.1 Console Application in Visual Studio 2019.

Install SuperSocket

Add the SuperSocket NuGet package (version 2.0.0-beta.8 or latter) to your project.

Server Implementation

using Microsoft.Extensions.Hosting;
using SuperSocket;
using SuperSocket.ProtoBase;
using System.Text;
using System.Threading.Tasks;
using System.Linq;

namespace TelnetServerExample
{
    class TelnetServer
    {
        static async Task Main()
        {
            var host = SuperSocketHostBuilder.Create<StringPackageInfo, CommandLinePipelineFilter>()
                .UsePackageHandler(async (session, pkg) =>
                {
                    int computationResult = 0;
                    string command = pkg.Key.ToUpper();

                    switch (command)
                    {
                        case "ADD":
                            computationResult = pkg.Parameters
                                .Select(param => int.Parse(param))
                                .Sum();
                            break;

                        case "SUB":
                            computationResult = pkg.Parameters
                                .Select(param => int.Parse(param))
                                .Aggregate((a, b) => a - b);
                            break;

                        case "MULT":
                            computationResult = pkg.Parameters
                                .Select(param => int.Parse(param))
                                .Aggregate((a, b) => a * b);
                            break;
                    }

                    await session.SendAsync(
                        Encoding.UTF8.GetBytes(computationResult.ToString() + "\r\n"));
                })
                .ConfigureSuperSocket(config =>
                {
                    config.Name = "Arithmetic Server";
                    config.Listeners = new[]
                    {
                        new ListenOptions
                        {
                            Ip = "Any",
                            Port = 4040
                        }
                    }.ToList();
                })
                .Build();

            await host.RunAsync();
        }
    }
}

Testing the Server

Connect to the server using a Telnet client:

telnet localhost 4040

Once connected, you can send commands in the format: ADD 5 10 or MULT 3 7.

Tags: SuperSocket NetCore Telnet Server-Side csharp

Posted on Sun, 17 May 2026 17:39:50 +0000 by theBond