C# SuperSocket 基础七【CountSpliterReceiveFilte-固定数量分隔符协议】不使用COMMAND
public class CountSpliterReceiveFilterSession : AppSession<CountSpliterReceiveFilterSession> { public override void Send(string message) { Console.WriteLine("发送消息:" + message); base.Send(message); } protected override void OnSessionStarted() { //输出客户端IP地址 Console.WriteLine(this.LocalEndPoint.Address.ToString()); this.Send("Hello User,Welcome to SuperSocket Telnet Server!"); } /// <summary> /// 连接关闭 /// </summary> /// <param name="reason"></param> protected override void OnSessionClosed(CloseReason reason) { base.OnSessionClosed(reason); } protected override void HandleUnknownRequest(StringRequestInfo requestInfo) { Console.WriteLine($"遇到未知的请求 Key:" + requestInfo.Key + $" Body:" + requestInfo.Body); base.HandleUnknownRequest(requestInfo); } /// <summary> /// 捕捉异常并输出 /// </summary> /// <param name="e"></param> protected override void HandleException(Exception e) { this.Send("error: {0}", e.Message); } }
public class CountSpliterReceiveFilterServer:AppServer<CountSpliterReceiveFilterSession> { /// 请求格式:#part1#part2#part3#part4#part5#part6#part7# public CountSpliterReceiveFilterServer() : base(new CountSpliterReceiveFilterFactory((byte)'#', 8)) //8个分隔符,7个参数。除使用默认的过滤工厂,还可以参照上一个实例定制协议 { } protected override bool Setup(IRootConfig rootConfig, IServerConfig config) { Console.WriteLine("正在准备配置文件"); return base.Setup(rootConfig, config); } protected override void OnStarted() { Console.WriteLine("服务已开始"); base.OnStarted(); } protected override void OnStopped() { Console.WriteLine("服务已停止"); base.OnStopped(); } /// <summary> /// 输出新连接信息 /// </summary> /// <param name="session"></param> protected override void OnNewSessionConnected(CountSpliterReceiveFilterSession session) { base.OnNewSessionConnected(session); //输出客户端IP地址 Console.Write("\r\n" + session.LocalEndPoint.Address.ToString() + ":连接"); } /// <summary> /// 输出断开连接信息 /// </summary> /// <param name="session"></param> /// <param name="reason"></param> protected override void OnSessionClosed(CountSpliterReceiveFilterSession session, CloseReason reason) { base.OnSessionClosed(session, reason); Console.Write("\r\n" + session.LocalEndPoint.Address.ToString() + ":断开连接"); } }
public class CountSpliterReceiveFilterServer:AppServer<CountSpliterReceiveFilterSession> { /// 请求格式:#part1#part2#part3#part4#part5#part6#part7# public CountSpliterReceiveFilterServer() : base(new CountSpliterReceiveFilterFactory((byte)'#', 8)) //8个分隔符,7个参数。除使用默认的过滤工厂,还可以参照上一个实例定制协议 { } }
static void Main(string[] args) { Console.WriteLine("请按任何键进行启动SuperSocket服务!"); Console.ReadKey(); Console.WriteLine(); var CountSpliterReceiveFilterServer = new CountSpliterReceiveFilterServer(); // 设置端口号 int port = 2017; //启动应用服务端口 if (!CountSpliterReceiveFilterServer.Setup(port)) //启动时监听端口2017 { Console.WriteLine("服务端口启动失败!"); Console.ReadKey(); return; } Console.WriteLine(); //注册连接事件 CountSpliterReceiveFilterServer.NewSessionConnected += CountSpliterReceiveFilterServer_NewSessionConnected; //注册请求事件 CountSpliterReceiveFilterServer.NewRequestReceived += CountSpliterReceiveFilterServer_NewRequestReceived; //注册Session关闭事件 CountSpliterReceiveFilterServer.SessionClosed += CountSpliterReceiveFilterServer_SessionClosed; //尝试启动应用服务 if (!CountSpliterReceiveFilterServer.Start()) { Console.WriteLine("服务启动失败!"); Console.ReadKey(); return; } Console.WriteLine("服务器状态:" + CountSpliterReceiveFilterServer.State.ToString()); Console.WriteLine("服务启动成功,请按'q'停止服务!"); while (Console.ReadKey().KeyChar != 'q') { Console.WriteLine(); continue; } //停止服务 CountSpliterReceiveFilterServer.Stop(); Console.WriteLine("服务已停止!"); Console.ReadKey(); } static void CountSpliterReceiveFilterServer_SessionClosed(CountSpliterReceiveFilterSession session, SuperSocket.SocketBase.CloseReason value) { Console.WriteLine(session.RemoteEndPoint.ToString() + "连接断开. 断开原因:" + value); } static void CountSpliterReceiveFilterServer_NewSessionConnected(CountSpliterReceiveFilterSession session) { Console.WriteLine(session.RemoteEndPoint.ToString() + " 已连接."); } /// <summary> /// 协议并没有什么太多复杂逻辑,不需要用到命令模式,直接用这种方式就可以了 /// </summary> /// <param name="session"></param> /// <param name="requestInfo"></param> private static void CountSpliterReceiveFilterServer_NewRequestReceived(CountSpliterReceiveFilterSession session, SuperSocket.SocketBase.Protocol.StringRequestInfo requestInfo) { Console.WriteLine(); Console.WriteLine("数据来源: " + session.RemoteEndPoint.ToString()); Console.WriteLine("接收数据内容:" + requestInfo.Body); }
来源:https://blog.csdn.net/ba_wang_mao/article/details/128286684