zoukankan      html  css  js  c++  java
  • 自定义AppServer

    TelnetSever.cs

     1     public class TelnetServer : AppServer<TelnetSession>
     2     {
     3         protected override bool Setup(IRootConfig rootConfig, IServerConfig config)
     4         {
     5             return base.Setup(rootConfig, config);
     6         }
     7 
     8         protected override void OnStarted()
     9         {
    10             base.OnStarted();
    11         }
    12 
    13         protected override void OnStopped()
    14         {
    15             base.OnStopped();
    16         }
    17     }
    View Code

    TelnetSession.cs

     1     public  class TelnetSession:AppSession<TelnetSession,StringRequestInfo>
     2     {
     3         protected override void OnSessionStarted()
     4         {
     5             Console.WriteLine(this.SessionID + ":Client connected");
     6             Send("Welcome to SuperSocket Telnet Server");
     7         }
     8 
     9         protected override void HandleUnknownRequest(SuperSocket.SocketBase.Protocol.StringRequestInfo requestInfo)
    10         {
    11             Send("Unknow request");
    12         }
    13 
    14         protected override void HandleException(Exception e)
    15         {
    16             this.Send("Application error: {0}", e.Message);
    17         }
    18 
    19         protected override void OnSessionClosed(CloseReason reason)
    20         {
    21             //add you logics which will be executed after the session is closed
    22             Console.WriteLine(this.SessionID + "Client Closed");
    23             base.OnSessionClosed(reason);
    24         }
    25     }
    View Code

    Add.cs

    1     public class Add : CommandBase<TelnetSession, StringRequestInfo>
    2     {
    3         public override void ExecuteCommand(TelnetSession session, StringRequestInfo requestInfo)
    4         {
    5             session.Send(requestInfo.Parameters.Select(p => Convert.ToInt32(p)).Sum().ToString());
    6         }
    7     }
    View Code

    Program.cs

     1     class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             Console.WriteLine("Press any key to start the server!");
     6 
     7             Console.ReadKey();
     8             Console.WriteLine();
     9             var appServer = new TelnetServer();
    10 
    11             var serverConfig = new ServerConfig
    12             {
    13                 Port = 8000 //set the listening port
    14             };
    15 
    16             //Setup the appServer
    17             if (!appServer.Setup(serverConfig))
    18             {
    19                 Console.WriteLine("Failed to setup!");
    20                 Console.ReadKey();
    21                 return;
    22             }
    23 
    24             Console.WriteLine();
    25 
    26             //Try to start the appServer
    27             if (!appServer.Start())
    28             {
    29                 Console.WriteLine("Failed to start!");
    30                 Console.ReadKey();
    31                 return;
    32             }
    33 
    34             Console.WriteLine("press key 'q' to stop it!");
    35 
    36             while (Console.ReadKey().KeyChar != 'q')
    37             {
    38                 Console.WriteLine();
    39                 continue;
    40             }
    41 
    42             Console.WriteLine();
    43 
    44             //Stop the appServer
    45             appServer.Stop();
    46         }
    47     }
    View Code
  • 相关阅读:
    Razor强类型视图下的文件上传
    Js Date对象
    Js用正则表达式验证字符串
    PLC软件: KW multiprog 和 codesys
    S7 200 下载程序块报错 "A compile error occurred , check non-fatal errors for more information " 大端模式
    WebOP Designer (在电脑上在线仿真)与 西门子 S7-200 PPI 通讯
    企业做iISO9001质量管理体系认证的步骤(通俗描述)
    .net framework 装不上的原因
    iso9001质量管理体系认证需要准备的材料&具体流程
    EPLAN 部件库中一个被容易遗漏的功能——“新变量”
  • 原文地址:https://www.cnblogs.com/caoyc/p/4707594.html
Copyright © 2011-2022 走看看