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

 

 posted on 2015-08-06 11:56  Just_Do  阅读(386)  评论(0编辑  收藏  举报