.net core 下运行 supersocket

 

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

namespace myApp
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var host = SuperSocketHostBuilder
            .Create<StringPackageInfo, CommandLinePipelineFilter>()
            .ConfigurePackageHandler(async (session, package) =>
            {
                var result = 0;
                try
                {
                    switch (package.Key.ToUpper())
                    {
                        case ("ADD"):
                            result = package.Parameters
                                .Select(p => int.Parse(p))
                                .Sum();
                            break;

                        case ("SUB"):
                            result = package.Parameters
                                .Select(p => int.Parse(p))
                                .Aggregate((x, y) => x - y);
                            break;

                        case ("MULT"):
                            result = package.Parameters
                                .Select(p => int.Parse(p))
                                .Aggregate((x, y) => x * y);
                            break;
                        default:
                            result = -1;
                            break;
                    }
                }
                catch (Exception ex)
                {
                }
                await session.SendAsync(Encoding.UTF8.GetBytes(result.ToString() + "\r\n"));
            })
            .ConfigureLogging((hostCtx, loggingBuilder) =>
            {
                loggingBuilder.AddConsole();
            })
            .ConfigureSuperSocket(options =>
            {
                options.Name = "Echo Server";
                options.Listeners = new[] {
                    new ListenOptions
                    {
                        Ip = "Any",
                        Port = 4040
                    }
                };
            }).Build();
            await host.RunAsync();
        }
    }
}

 

参考资料:https://docs.supersocket.net/v2-0/zh-CN/A-Telnet-Example

posted on 2021-02-24 16:50  wangzhiliang  阅读(1517)  评论(0编辑  收藏  举报

导航