创建简单的Telnet实例
2024-08-25 22:24:50
step1.先加入库SuperSocket.Common.dll, SuperSocket.SocketBase.dll, SuperSocket.SocketEngine.dll,log4net.dll,System.Threading.dll。同时还需引入NET中的System.Configuration。如果要使用默认日志框架,需要复制Config文件夹
step2.代码实现
class Program
{
static void Main(string[] args)
{
Console.WriteLine("press any key to start the server");
Console.ReadKey();
Console.WriteLine();
AppServer server = new AppServer();
if (!server.Setup())
{
Console.WriteLine("Failed to setup");
Console.ReadKey();
return;
} server.NewSessionConnected += new SessionHandler<AppSession>(server_NewSessionConnected);
server.NewRequestReceived += new RequestHandler<AppSession, SuperSocket.SocketBase.Protocol.StringRequestInfo>(server_NewRequestReceived); if (!server.Start())
{
Console.WriteLine("Failed to start");
Console.ReadKey();
return;
} Console.WriteLine("The server started successfully, press key 'q' to stop it!"); while (Console.ReadKey().KeyChar != 'q')
{
Console.WriteLine();
continue;
}
Console.WriteLine();
server.Stop();
Console.WriteLine("The server was stopped!");
} static void server_NewRequestReceived(AppSession session, SuperSocket.SocketBase.Protocol.StringRequestInfo requestInfo)
{
Console.WriteLine(requestInfo.Key + " " + requestInfo.Body);
switch (requestInfo.Key.ToUpper())
{
case ("ECHO"):
string r = requestInfo.Body;
session.Send(r);
Console.WriteLine(r);
break; case ("ADD"):
string r2= requestInfo.Parameters.Select(p => Convert.ToInt32(p)).Sum().ToString();
session.Send(r2);
Console.WriteLine(r2);
break; case ("MULT"): var result = ; foreach (var factor in requestInfo.Parameters.Select(p => Convert.ToInt32(p)))
{
result *= factor;
} session.Send(result.ToString());
Console.WriteLine(result);
break;
}
} static void server_NewSessionConnected(AppSession session)
{
Console.WriteLine(session.SessionID + ":connect successed");
}
}
Step3.测试:代码测试:在window中运行里面输入telnet 127.0.0.1 8000 然后按回车就可以了
最新文章
- poj[1187][Noi 01]陨石的秘密
- docker-image container 基本操作 -常用命令
- oracle 10g在redhat5下的安装
- WPF:实现主应用程序单一实例运行方式总结
- wikioi 3116 高精度练习之加法
- 截获控制台程序关闭事件(SetConsoleCtrlHandler)
- Blend4精选案例图解教程(一):丰富的形状(Shape)资源
- 2017-2-17 c#基础学习 (控制台程序的创建,输出,输入,定义变量,变量赋值,值覆盖,值拼接,值打印)
- Erlang调度器细节探析
- GIS大数据存储预研
- css3 @media支持ie8用respond.js 解决IE6~8的响应式布局问题
- Centos7上vsftp脚本-->; sh vsftp.sh 用户名 密码 -->; sh vsftp.sh install
- 调用WebService的简单方法
- 推广App篇
- Windows 下安装 swoole 具体步骤(php)
- LeetCode 905 Sort Array By Parity 解题报告
- Ext.net combobox 的disabled
- 配置Oracle E-Business Suite Integrated SOA Gateway Release 12.1.2/12.1.3
- js正则表达式实现手机号码,密码正则验证
- testlogin