首页 > 编程语言 >C# SuperSocket 基础七【CountSpliterReceiveFilte-固定数量分隔符协议】不使用COMMAND

C# SuperSocket 基础七【CountSpliterReceiveFilte-固定数量分隔符协议】不使用COMMAND

时间:2024-10-28 16:01:03浏览次数:4  
标签:Console SuperSocket C# void session COMMAND WriteLine override CountSpliterRecei

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

 

标签:Console,SuperSocket,C#,void,session,COMMAND,WriteLine,override,CountSpliterRecei
From: https://www.cnblogs.com/ywtssydm/p/18510828

相关文章

  • IIC通信协议详解 & PCF8591应用(Verilog实现)
    该文章结合PCF85918-bitAD/DA模数/数模转换器来详细介绍IIC通信协议,尽量做到条理清晰,通俗易懂。该文图片均从PCF8591手册中截取,一定程度上引导读者学习阅读datasheet。1.PCF8591引脚2.功能介绍2.1地址位在I2C总线系统中,每个PCF8591设备都通过发送一个有效地址来激活。......
  • computed
    计算属性关键词:computed。计算属性在处理一些复杂逻辑时是很有用的。在Vue.js中,computed属性是一种强大的特性,用于定义依赖于其他响应式数据的计算值。computed属性不仅能够简化模板中的表达式,还能够缓存计算结果,避免不必要的重复计算,从而提高性能。本文将深入探讨Vue......
  • elasticsearch使用
    1、选择1、ElasticsearchRestTemplate是spring对官方HighLevelRESTClient的封装。2、ElasticSearch8.x弃用了HighLevelRESTClient,移除了JavaTransportClient,推荐使用ElasticsearchJavaAPI(后续使用8的建议使用ElasticsearchJavaAPI)2、ElasticsearchRestTemp......
  • H7-TOOL测试凌欧LKS32MC03x的骚操作玩法支持情况,不接硬件复位引脚,SWD上电100ms复用其
    【背景】这个骚操作,骚就骚在SWD接口引脚复用其他功能了,板子还没有把硬件复位引脚接出来,硬件复位引脚是专治这种场景的。虽然客户的程序搞了个上电后延迟100ms才复用,但JLINK,ULINK,STLINK,DAPLINK在MDK下都已经无法控制这个芯片下载,但使用第3方的烧录可以下载,且客户近期不方便提......
  • C# SuperSocket 基础六【CountSpliterReceiveFilte-固定数量分隔符协议】使用COMMAND
    publicclassCountSpliterReceiveFilterSession:AppSession<CountSpliterReceiveFilterSession>{publicoverridevoidSend(stringmessage){Console.WriteLine("发送消息:"+message);base.Send(message)......
  • CSP游记
    Day-1下午与同学一同去试机,结果ST与家里装的插件不同,只能用DEV了,接着看了看22年的题目,想出来了T4部分分后就走了。晚上在机房颓废,不知道干什么,就重新写了遍动态开点,挂了,调了半个小时才过。Day1上午提前20分钟进了机房,打完模板后写了个最短路板子。前面两道题都......
  • Surface Splatting
    AbstractModernlaserrangeandopticalscannersneedrenderingtechniquesthatcanhandlemillionsofpointswithhighresolutiontextures.Thispaperdescribesapointrenderingandtexturefilteringtechniquecalledsurfacesplattingwhichdirectlyren......
  • A. Build a Computer
    独立做出了银牌题~线段树划分区间其实无须左闭右开点击查看代码#include<bits/stdc++.h>usingnamespacestd;inttot,id;vector<int>a[1005],c[1005];intt[1005][2];voidadd(intu,intv,intw){a[u].push_back(v);c[u].push_back(w);}voidask(intl,......
  • Gaussian Splatting with NeRF-based Color and Opacity
    AbstractNeRFs havedemonstratedtheremarkablepotentialofneuralnetworkstocapturetheintricaciesof3Dobjects.Byencodingtheshapeandcolorinformationwithinneuralnetworkweights,NeRFsexcelatproducingstrikinglysharpnovelviewsof3Do......
  • 扣子(Coze)
    一、产品描述扣子(英文名称为Coze)是由字节跳动推出的新一代一站式AIBot开发平台。它于2024年2月1日正式上线,旨在让非开发者也能快速创建、调试和优化聊天机器人。扣子平台基于大模型技术,客户无需具备深厚的编程基础,即可在平台上轻松搭建和发布各类智能Bot。体验地址:扣子(Coze)......