C#SuperSocket的使用说明
C#SuperSocket服务器的简易实现
上一篇文章我们使用原生的socket分别实现了服务器和客户端,
本篇文章使用SuperSocket来开发实现服务器,
之前也介绍了SuperSocket是一个轻量级, 跨平台而且可扩展的 .Net/Mono Socket 服务器程序框架。你无须了解如何使用 Socket, 如何维护 Socket 连接和 Socket 如何工作,但是你却可以使用 SuperSocket 很容易的开发出一款 Socket 服务器端软件,例如游戏服务器,GPS 服务器, 工业控制服务和数据采集服务器等等。
接下来开始我们的开发,首先我们需要安装SuperSocket相关程序包,我们新建一个项目开发SuperSocket服务器
然后打开NuGet程序包管理器,搜索SuperSocket ,下载安装SuperSocket和SuperSocket.Engine
下载安装完毕后,我们的项目中会自动引用了SuperSocke和log4net 相关程序集和配置文件
进入正题上代码,我们这里只用SuperSocket做服务器端,客户端使用SocketTool做测试
SocketTool
链接:https://pan.baidu.com/s/1ykEofUIZKE2yhe3mMyRbJw
提取码:m2nk
SuperSocket实现服务器:

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using System.Net.Sockets; 11 using System.Net; 12 using System.Threading; 13 using SuperSocket; 14 using SuperSocket.SocketBase; 15 using SuperSocket.SocketBase.Protocol; 16 17 namespace SuperSocket 18 { 19 public partial class SuperSocketServer : Form 20 { 21 public SuperSocketServer() 22 { 23 InitializeComponent(); 24 } 25 26 private void SuperSocketServer_Load(object sender, EventArgs e) 27 { 28 //txt_ip.Text = "127.0.0.1"; 29 txt_port.Text = "3333"; 30 } 31 32 //AppServer 代表了监听客户端连接,承载TCP连接的服务器实例。理想情况下,我们可以通过AppServer实例获取任何你想要的客户端连接,服务器级别的操作和逻辑应该定义在此类之中。 33 AppServer appServer; 34 //缓冲字节数组 35 byte[] buffer = new byte[2048]; 36 37 string ipAddress_Connect; 38 string ipAddress_Close; 39 string ipAddress_Receive; 40 41 //存储session和对应ip端口号的泛型集合 42 Dictionary<string, AppSession> sessionList = new Dictionary<string, AppSession>(); 43 44 enum OperateType 45 { 46 47 Add = 1, //添加 48 Remove = 2 //移除 49 } 50 51 /// <summary> 52 /// 开启服务 53 /// </summary> 54 /// <param name="sender"></param> 55 /// <param name="e"></param> 56 private void btn_StartListen_Click(object sender, EventArgs e) 57 { 58 appServer = new AppServer(); 59 if (!appServer.Setup(int.Parse(txt_port.Text))) 60 { 61 SetMessage("Failed to Setup"); 62 return; 63 } 64 if (!appServer.Start()) 65 { 66 SetMessage("Failed to Start"); 67 return; 68 } 69 else 70 { 71 SetMessage("开启监听"); 72 } 73 //SuperSocket自定义了三个事件 ,连接事件,接收事件,关闭事件 74 appServer.NewSessionConnected += appServer_NewSessionConnected; 75 appServer.NewRequestReceived += appServer_NewRequestReceived; 76 appServer.SessionClosed += appServer_SessionClosed; 77 } 78 79 /// <summary> 80 /// 接收连接 81 /// </summary> 82 /// <param name="session"></param> 83 void appServer_NewSessionConnected(AppSession session) 84 { 85 //有新连接的时候,添加记录 session.LocalEndPoint属性获取当前session的ip和端口号 86 //AppSession 代表一个和客户端的逻辑连接,基于连接的操作应该定于在该类之中。你可以用该类的实例发送数据到客户端,接收客户端发送的数据或者关闭连接。 87 88 //获取远程客户端的ip端口号 89 ipAddress_Connect = session.RemoteEndPoint.ToString(); 90 ComboboxHandle(ipAddress_Connect, OperateType.Add); 91 sessionList.Add(ipAddress_Connect, session); 92 SetMessage(ipAddress_Connect + "已连接!"); 93 } 94 95 /// <summary> 96 /// 接收数据 97 /// </summary> 98 /// <param name="session"></param> 99 /// <param name="requestInfo"></param> 100 void appServer_NewRequestReceived(AppSession session, StringRequestInfo requestInfo) 101 { 102 //requestInfo.Key 是请求的命令行用空格分隔开的第一部分 103 //requestInfo.Parameters 是用空格分隔开的其余部分 104 //requestInfo.Body 是出了请求头之外的所有内容 105 ipAddress_Receive = session.RemoteEndPoint.ToString(); 106 SetMessage("收到" + ipAddress_Receive + "数据: "+requestInfo.Key +" "+ requestInfo.Body); 107 } 108 109 /// <summary> 110 /// 关闭连接 111 /// </summary> 112 /// <param name="session"></param> 113 /// <param name="value"></param> 114 void appServer_SessionClosed(AppSession session, SocketBase.CloseReason value) 115 { 116 ipAddress_Close = session.RemoteEndPoint.ToString(); 117 ComboboxHandle(ipAddress_Close, OperateType.Remove); 118 sessionList.Remove(ipAddress_Close); 119 SetMessage(ipAddress_Close + "已关闭连接!"); 120 } 121 /// <summary> 122 /// 发送数据 123 /// </summary> 124 /// <param name="sender"></param> 125 /// <param name="e"></param> 126 private void btn_send_Click(object sender, EventArgs e) 127 { 128 //从客户端列获取想要发送数据的客户端的ip和端口号,然后从sessionList中获取对应session然后调用send()发送数据 129 if (cmb_socketlist.Items.Count != 0) 130 { 131 if (cmb_socketlist.SelectedItem == null) 132 { 133 MessageBox.Show("请选择一个客户端发送数据!"); 134 return; 135 } 136 else 137 { 138 sessionList[cmb_socketlist.SelectedItem.ToString()].Send(txt_send.Text); 139 } 140 } 141 else 142 { 143 SetMessage("当前没有正在连接的客户端!"); 144 } 145 txt_send.Clear(); 146 } 147 148 /// <summary> 149 /// 添加信息 150 /// </summary> 151 /// <param name="str"></param> 152 private void SetMessage(string str) 153 { 154 richTextBox1.Invoke(new Action(() => { richTextBox1.AppendText(str + "\r\n"); })); 155 } 156 157 /// <summary> 158 /// combobox操作 159 /// </summary> 160 /// <param name="ipAddress"></param> 161 /// <param name="operateType">add 添加项/remove 移除项</param> 162 private void ComboboxHandle(string ipAddress, OperateType operateType) 163 { 164 if (operateType == OperateType.Add) 165 { 166 cmb_socketlist.Invoke(new Action(() => { cmb_socketlist.Items.Add(ipAddress); })); 167 } 168 if (operateType == OperateType.Remove) 169 { 170 cmb_socketlist.Invoke(new Action(() => { cmb_socketlist.Items.Remove(ipAddress); })); 171 } 172 } 173 174 } 175 }
先挂上官方说明文档 http://docs.supersocket.net/v1-6/zh-CN
这里说明几点:
(1)这里appServer_NewRequestReceived(AppSession session, StringRequestInfo requestInfo)方法中的StringRequestInfo
是包含请求信息的,
requestInfo.Key 是请求的命令行用空格分隔开的第一部分
requestInfo.Parameters 是用空格分隔开的其余部分,用空格分割开的字符串数组
requestInfo.Body 是出了请求头之外的所有内容,是一个字符串
(2)这里requestInfo是客户端发送过来 严格按照 请求头 请求参数 请求参数 请求参数 \r\n 的格式发送, 空格隔开的第一部分是请求头,后边用空格分割后组成的数据就是请求参数
而且必须是以回车换行结尾 SuperSocket才能正确接收;
(3)这里请求头和请求参数用什么分割是可以自定义;我们可以自定义AppServer类,继承APPServer类,然后使用下面的代码扩展命令行协议
比如用":"分割请求头和请求参数,用","分隔请求参数.

1 public class YourServer : AppServer<YourSession> 2 { 3 public YourServer() 4 : base(new CommandLineReceiveFilterFactory(Encoding.Default, new BasicRequestInfoParser(":", ","))) 5 { 6 7 } 8 }
接下来我们开始测试,还是默认使用3333端口,开启监听,我们依旧是使用SocketTool工具创建三个客户端,一起访问服务器
服务器:
客户端
接下来三个客户端分别以"9100"为请求头,test为请求体给服务器发送数据,记住客户端发送数据一定以回车换行为结尾
客户端:
服务器:
接下里测试服务器给客户端,这里以服务器给端口为1083的客户端发送数据"aaaa"
从客户端列选择端口号为1083的客户端,在textbox输入aaaa 发送数据
服务器
客户端
接下里客户端关闭连接
服务器
到此,SuperSocket实现的服务器测试完美收官,其实SuperSocket的功能远不止于此,我也只是刚开始使用
待后续研究官方文档后什么新的发现在更新,告辞!
感谢客观阅读,拜谢(抱拳~)
两篇文章的源码
本来想上传GitHub的,毕竟这样显得专业一点,奈何初来乍到的,实在操作不了(留下了不懂英文的泪水),还是放云盘吧!
链接:https://pan.baidu.com/s/1zjCvkP2Ne9U3KR8vyBKhFw
提取码:gee7
出处:https://www.cnblogs.com/pandefu/p/10774780.html
=======================================================================================
C#SuperSocket的搭建--通过配置启动
之前我们借助一个SuperSocket实现了一个简易版的服务器, 但是不管是Server还是Session都是使用框架的,本篇博客我们要实现自己的Server和Session,来重写框架原生的Server或Session的方法,或添加自己所需的属性,来实现自己的业务逻辑,并且也不在使用事件来绑定接收,连接,或关闭事件,全部交给Bootstrap来执行,(这个Bootstrap并不是指前端框架的Bootstrap ,而是指的SuperSocket框架的一个引导程序或说是辅助程序),就是这里我们会使用Bootstrap 来配置启动SuperSocket;
本篇文章皆为我阅读官方文档后总结实现,所以很多代码是直接搬的官方文档的,我的主要目的是为了能实现并运行SuperSocket服务器,所以建议优先阅读官方文档
官方文档:http://docs.supersocket.net/v1-6/zh-CN
SuperSocket 是一个轻量级, 跨平台而且可扩展的 .Net/Mono Socket 服务器程序框架。你无须了解如何使用 Socket, 如何维护 Socket 连接和 Socket 如何工作,但是你却可以使用 SuperSocket 很容易的开发出一款 Socket 服务器端软件,例如游戏服务器,GPS 服务器, 工业控制服务和数据采集服务器等等。
怎么从NuGet安装SuperSocket就不再赘述了,我们直接看实现
首先我们可以按自己需求定义自己APPSession(因为我也不知道我自己定义的Session中应该有什么方法,什么属性,所以照搬官方文档了~~~)

1 using SuperSocket.SocketBase; 2 using SuperSocket.SocketBase.Protocol; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace SuperSocket2.Session 10 { 11 public class MySession : AppSession<MySession> 12 { 13 protected override void OnSessionStarted() 14 { 15 this.Send("Welcome to SuperSocket Telnet Server"); 16 } 17 18 protected override void HandleUnknownRequest(StringRequestInfo requestInfo) 19 { 20 this.Send("Unknow request"); 21 } 22 23 protected override void HandleException(Exception e) 24 { 25 this.Send("Application error: {0}", e.Message); 26 } 27 28 protected override void OnSessionClosed(CloseReason reason) 29 { 30 //add you logics which will be executed after the session is closed 31 base.OnSessionClosed(reason); 32 } 33 } 34 }
接着按自己需求定义自己APPServer,

1 using SuperSocket.SocketBase; 2 using SuperSocket.SocketBase.Config; 3 using SuperSocket.SocketBase.Protocol; 4 using SuperSocket2.Session; 5 using System; 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 11 namespace SuperSocket2.Server 12 { 13 public class MyServer : AppServer<MySession> 14 { 15 16 public MyServer() 17 : base(new CommandLineReceiveFilterFactory(Encoding.Default, new BasicRequestInfoParser(":", ","))) 18 { 19 20 } 21 22 protected override bool Setup(IRootConfig rootConfig, IServerConfig config) 23 { 24 return base.Setup(rootConfig, config); 25 } 26 27 protected override void OnStartup() 28 { 29 base.OnStartup(); 30 } 31 32 protected override void OnStopped() 33 { 34 base.OnStopped(); 35 } 36 } 37 }
自定义的APPserver,在继承APPServer是的Session泛型,记得要更改为我们自定义的Session上一篇文章我们也说道,它默认的请求的 key 和 body 通过字符 ' ' 空格分隔, 因需求不同 我们可以将它改为 ':' 分隔 ,而且多个参数被字符 ',' 分隔,所以我们在修改了无参构造函数,来实现拓展命令行协议;
接下来要做的
所以我们来自己写一个命令类

1 using SuperSocket.SocketBase.Command; 2 using SuperSocket.SocketBase.Protocol; 3 using SuperSocket2.Session; 4 using System; 5 using System.Collections.Generic; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using System.Threading; 11 12 namespace SuperSocket2.Command 13 { 14 /// <summary> 15 /// 处理请求头为6003的命令 16 /// </summary> 17 public class CommandOne : CommandBase<MySession, StringRequestInfo> 18 { 19 public override string Name 20 { 21 get 22 { 23 return "6003"; 24 } 25 } 26 27 public override void ExecuteCommand(MySession session, StringRequestInfo requestInfo) 28 { 29 //向客户端返回信息,已接受到6003命令 30 s.Send("Order 6003 received"); 31 } 32 33 } 34 } 35
请求处理代码必须被放置于方法 "ExecuteCommand(TAppSession session, TRequestInfo requestInfo)" 之中,并且属性 "Name" 的值用于匹配接收到请求实例(requestInfo)的Key。当一个请求实例(requestInfo) 被收到时,SuperSocket 将会通过匹配请求实例(requestInfo)的Key和命令的Name的方法来查找用于处理该请求的命令
但是由于类名的命名必须有字母数字下划线组成,且数字不能开头,如果要接收请求的Key为6003,我们就需要做一些修改
所以这里我重写了Name方法,这样,请求的Key是6003 也能触发CommandOne命令
好了,我们的自定义Server,Session,命令都写完了,接下来需要我们使用Bootstrap来配置启动,我们这里只为了保证SuperSocket能正常启动,所以不做多余的配置(☞配置示例)
修改App.config文件,添加<configuration>节点和<superSocket>节点
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <configSections> 4 <section name="superSocket" 5 type="SuperSocket.SocketEngine.Configuration.SocketServiceConfig, SuperSocket.SocketEngine" /> 6 </configSections> 7 <superSocket> 8 <servers> 9 <!--serverType中,逗号左边的是你自定义的server在项目中的位置,逗号右边是项目名,ip就是服务器ip,port端口号--> 10 <server name="TelnetServer" 11 serverType="SuperSocket2.Server.MyServer,SuperSocket2" 12 ip="Any" port="3666"> 13 </server> 14 </servers> 15 </superSocket> 16 <startup> 17 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 18 </startup> 19 </configuration>
配置完毕,我们启动程序,在Form_load中实例化bootstrap,启动服务(原谅我懒,实在不愿意对这个Form美化了,就加了一个Richtextbox,显示一下是否初始化成功,启动成功)

1 using SuperSocket.SocketBase; 2 using SuperSocket.SocketEngine; 3 using System; 4 using System.Collections.Generic; 5 using System.ComponentModel; 6 using System.Data; 7 using System.Drawing; 8 using System.Linq; 9 using System.Text; 10 using System.Threading.Tasks; 11 using System.Windows.Forms; 12 13 14 15 namespace SuperSocket2 16 { 17 public partial class Form1 : Form 18 { 19 public Form1() 20 { 21 InitializeComponent(); 22 } 23 private void Form1_Load(object sender, EventArgs e) 24 { 25 //声明bootStrap实例 26 var bootstrap = BootstrapFactory.CreateBootstrap(); 27 //初始化 28 if (!bootstrap.Initialize()) 29 { 30 SetMessage("Failed to initialize!"); 31 return; 32 } 33 //开启服务 34 var result = bootstrap.Start(); 35 36 if (result == StartResult.Failed) 37 { 38 SetMessage("Failed to start!"); 39 40 return; 41 } 42 else 43 { 44 SetMessage("服务器启动成功"); 45 } 46 //bootstrap.Stop(); 47 48 } 49 50 public void SetMessage(string msg) 51 { 52 this.richTextBox1.Invoke(new Action(() => { this.richTextBox1.AppendText(msg + "\r\n"); })); 53 } 54 55 } 56 }
好,一个简单的,完整的自定义SuperSocket就完成了,我们运行,借助TCP/UDP Socket调试工具执行6003命令试一下
这里说明一下,SuperSocket框架的命令行协议定义了每个请求必须以回车换行结尾 "\r\n";
所以我们输完6003:hello命令后,记得加回车;
测试完成,简易SuperSocket框架搭建成功
以上为我自己学习总结并实现,有错误之处,希望大家不吝赐教,感谢(抱拳)!
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/17571580.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!
posted on 2023-07-21 15:42 jack_Meng 阅读(1553) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2014-07-21 系列文章--SharePoint 开发教程