程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 創建簡單的Telnet實例,telnet實例

創建簡單的Telnet實例,telnet實例

編輯:C#入門知識

創建簡單的Telnet實例,telnet實例


step1.先加入庫SuperSocket.Common.dll, SuperSocket.SocketBase.dll, SuperSocket.SocketEngine.dll,log4net.dll,System.Threading.dll。同時還需引入NET中的System.Configuration。如果要使用默認日志框架,需要復制Config文件夾

 

step2.代碼實現

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Console.WriteLine("press any key to start the server");
 6             Console.ReadKey();
 7             Console.WriteLine();
 8             AppServer server = new AppServer();
 9             if (!server.Setup(8000))
10             {
11                 Console.WriteLine("Failed to setup");
12                 Console.ReadKey();
13                 return;
14             }
15 
16             server.NewSessionConnected += new SessionHandler<AppSession>(server_NewSessionConnected);
17             server.NewRequestReceived += new RequestHandler<AppSession, SuperSocket.SocketBase.Protocol.StringRequestInfo>(server_NewRequestReceived);
18 
19             if (!server.Start())
20             {
21                 Console.WriteLine("Failed to start");
22                 Console.ReadKey();
23                 return;
24             }
25 
26             Console.WriteLine("The server started successfully, press key 'q' to stop it!");
27             
28              while (Console.ReadKey().KeyChar != 'q')
29             {
30                 Console.WriteLine();
31                 continue;
32             }
33              Console.WriteLine();
34             server.Stop();
35             Console.WriteLine("The server was stopped!");
36         }
37 
38         static void server_NewRequestReceived(AppSession session, SuperSocket.SocketBase.Protocol.StringRequestInfo requestInfo)
39         {
40             Console.WriteLine(requestInfo.Key + " " + requestInfo.Body);
41             switch (requestInfo.Key.ToUpper())
42             {
43                 case ("ECHO"):
44                     string r = requestInfo.Body;
45                     session.Send(r);
46                     Console.WriteLine(r);
47                     break;
48 
49                 case ("ADD"):
50                     string r2= requestInfo.Parameters.Select(p => Convert.ToInt32(p)).Sum().ToString();
51                     session.Send(r2);
52                     Console.WriteLine(r2);
53                     break;
54 
55                 case ("MULT"):
56 
57                     var result = 1;
58 
59                     foreach (var factor in requestInfo.Parameters.Select(p => Convert.ToInt32(p)))
60                     {
61                         result *= factor;
62                     }
63 
64                     session.Send(result.ToString());
65                     Console.WriteLine(result);
66                     break;
67             }
68         }
69 
70         static void server_NewSessionConnected(AppSession session)
71         {
72             Console.WriteLine(session.SessionID + ":connect successed");
73         }
74     }

 

Step3.測試:代碼測試:在window中運行裡面輸入telnet 127.0.0.1 8000  然後按回車就可以了

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved