SuperSocket框架中BinaryRequestInfo协议的使用

一、开发环境

  1.Windows 10 企业版 64位

  2.Microsoft Visual Studio 2017 企业版

二、项目开始

  1.新建控制台程序,项目名称“BinarySuperSocket”,.net框架“4.7.1”

  

  2.安装SuperSocket的包,点击 “工具->NuGet包管理器->程序包管理器控制台”

  

   输入“install-package supersocket”,然后回车,提示安装成功,见下图

     

  

  输入“install-package supersocket.engine”,然后回车,提示安装成功,见下图

   

  

  3.新建BinarySession类

  

1using SuperSocket.SocketBase; 2using SuperSocket.SocketBase.Protocol; 3 4namespace BinarySuperSocket 5{ 6 public class BinarySession : AppSession<BinarySession, BinaryRequestInfo> 7 { 8 } 9}

BinarySession类

   4.新建BinaryServer类

  

1using SuperSocket.SocketBase; 2using SuperSocket.SocketBase.Protocol; 3 4namespace BinarySuperSocket 5{ 6 public class BinaryServer : AppServer<BinarySession, BinaryRequestInfo> 7 { 8 9 public BinaryServer(IReceiveFilterFactory<BinaryRequestInfo> protocol) : base(protocol) 10 { 11 } 12 } 13}

BinaryServer类

  5.新建BinaryReceiveFilter类

  

1using System; 2using SuperSocket.SocketBase.Protocol; 3 4namespace BinarySuperSocket 5{ 6 public class BinaryReceiveFilter : IReceiveFilter<BinaryRequestInfo> 7 { 8 public int LeftBufferSize { get; set; } 9 10 public IReceiveFilter<BinaryRequestInfo> NextReceiveFilter { get; set; } 11 12 public FilterState State { get; set; } 13 14 public BinaryRequestInfo Filter(byte[] readBuffer, int offset, int length, bool toBeCopied, out int rest) 15 { 16 byte[] value = new byte[length]; 17 Array.Copy(readBuffer, offset, value, 0, length); 18 BinaryRequestInfo binaryRequestInfo = new BinaryRequestInfo("key", value); 19 rest = length - value.Length; 20 return binaryRequestInfo; 21 } 22 23 public void Reset() 24 { 25 } 26 } 27}

BinaryReceiveFilter类

  6.新建BinaryReceiveFilterFactory类

  

1using System.Net; 2using SuperSocket.SocketBase; 3using SuperSocket.SocketBase.Protocol; 4 5namespace BinarySuperSocket 6{ 7 public class BinaryReceiveFilterFactory : IReceiveFilterFactory<BinaryRequestInfo> 8 { 9 public IReceiveFilter<BinaryRequestInfo> CreateFilter(IAppServer appServer, IAppSession appSession, IPEndPoint remoteEndPoint) 10 { 11 BinaryReceiveFilter binaryReceiveFilter = new BinaryReceiveFilter(); 12 return binaryReceiveFilter; 13 } 14 } 15}

BinaryReceiveFilterFactory类

  7.Main函数调用

  

1using System; 2using System.Text; 3 4namespace BinarySuperSocket 5{ 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 BinaryReceiveFilterFactory filterFactory = new BinaryReceiveFilterFactory(); 11 BinaryServer server = new BinaryServer(filterFactory); 12 13 server.NewSessionConnected += Server_NewSessionConnected; 14 server.SessionClosed += Server_SessionClosed; 15 server.NewRequestReceived += Server_NewRequestReceived; 16 17 bool b = server.Setup(7788);//设置端口号 18 Console.WriteLine($"设置端口号结果:{b}"); 19 20 if (b) 21 { 22 bool s = server.Start();//开始服务 23 Console.WriteLine($"服务开启结果:{s}"); 24 } 25 26 Console.ReadKey(); 27 } 28 29 private static void Server_NewRequestReceived(BinarySession session, SuperSocket.SocketBase.Protocol.BinaryRequestInfo requestInfo) 30 { 31 StringBuilder sb = new StringBuilder(); 32 Array.ForEach(requestInfo.Body, b => sb.Append($"{b} ")); 33 Console.WriteLine($"接收到客户端 {session.Config.Ip}:{session.Config.Port} 的数据:"); 34 Console.WriteLine($"字节数组形式:{sb.ToString()}"); 35 Console.WriteLine($" ASCII码转换:{Encoding.ASCII.GetString(requestInfo.Body)}"); 36 } 37 38 private static void Server_SessionClosed(BinarySession session, SuperSocket.SocketBase.CloseReason value) 39 { 40 Console.WriteLine($"客户端 {session.Config.Ip}:{session.Config.Port} 断开,原因:{value.ToString()}"); 41 } 42 43 private static void Server_NewSessionConnected(BinarySession session) 44 { 45 Console.WriteLine($"客户端 {session.Config.Ip}:{session.Config.Port} 已连接"); 46 } 47 } 48}

Main函数调用

  8.运行

  

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )