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
  • 相关阅读:
    【objective-c】字典快速转换为Model代码
    【objective-c】类目 延展 协议
    【objective-c】内存管理原则
    UI基础-UI基础控件(一)
    OC面向对象-OC基础早知道
    我对于编程培训班的一些看法
    如何为SQLSERVER查询分析器开启事务
    准备在博客园常驻了
    Spring学习(二)(1.Spring依赖注入的方式 2.依赖注入的类型 3.Bean的作用域 4.自动注入 5在Spring配置文件中引入属性文件6使用注解的方式 )
    Springmvc的常用注解
  • 原文地址:https://www.cnblogs.com/caoyc/p/4707594.html
Copyright © 2011-2022 走看看