一、开发环境

  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类

  

  

using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Protocol; namespace BinarySuperSocket
{
public class BinarySession : AppSession<BinarySession, BinaryRequestInfo>
{
}
}

BinarySession类

  4.新建BinaryServer类

  

using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Protocol; namespace BinarySuperSocket
{
public class BinaryServer : AppServer<BinarySession, BinaryRequestInfo>
{ public BinaryServer(IReceiveFilterFactory<BinaryRequestInfo> protocol) : base(protocol)
{
}
}
}

BinaryServer类

  5.新建BinaryReceiveFilter类

  

using System;
using SuperSocket.SocketBase.Protocol; namespace BinarySuperSocket
{
public class BinaryReceiveFilter : IReceiveFilter<BinaryRequestInfo>
{
public int LeftBufferSize { get; set; } public IReceiveFilter<BinaryRequestInfo> NextReceiveFilter { get; set; } public FilterState State { get; set; } public BinaryRequestInfo Filter(byte[] readBuffer, int offset, int length, bool toBeCopied, out int rest)
{
byte[] value = new byte[length];
Array.Copy(readBuffer, offset, value, , length);
BinaryRequestInfo binaryRequestInfo = new BinaryRequestInfo("key", value);
rest = length - value.Length;
return binaryRequestInfo;
} public void Reset()
{
}
}
}

BinaryReceiveFilter类

  6.新建BinaryReceiveFilterFactory类

  

using System.Net;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Protocol; namespace BinarySuperSocket
{
public class BinaryReceiveFilterFactory : IReceiveFilterFactory<BinaryRequestInfo>
{
public IReceiveFilter<BinaryRequestInfo> CreateFilter(IAppServer appServer, IAppSession appSession, IPEndPoint remoteEndPoint)
{
BinaryReceiveFilter binaryReceiveFilter = new BinaryReceiveFilter();
return binaryReceiveFilter;
}
}
}

BinaryReceiveFilterFactory类

  7.Main函数调用

  

using System;
using System.Text; namespace BinarySuperSocket
{
class Program
{
static void Main(string[] args)
{
BinaryReceiveFilterFactory filterFactory = new BinaryReceiveFilterFactory();
BinaryServer server = new BinaryServer(filterFactory); server.NewSessionConnected += Server_NewSessionConnected;
server.SessionClosed += Server_SessionClosed;
server.NewRequestReceived += Server_NewRequestReceived; bool b = server.Setup();//设置端口号
Console.WriteLine($"设置端口号结果:{b}"); if (b)
{
bool s = server.Start();//开始服务
Console.WriteLine($"服务开启结果:{s}");
} Console.ReadKey();
} private static void Server_NewRequestReceived(BinarySession session, SuperSocket.SocketBase.Protocol.BinaryRequestInfo requestInfo)
{
StringBuilder sb = new StringBuilder();
Array.ForEach(requestInfo.Body, b => sb.Append($"{b} "));
Console.WriteLine($"接收到客户端 {session.Config.Ip}:{session.Config.Port} 的数据:");
Console.WriteLine($"字节数组形式:{sb.ToString()}");
Console.WriteLine($" ASCII码转换:{Encoding.ASCII.GetString(requestInfo.Body)}");
} private static void Server_SessionClosed(BinarySession session, SuperSocket.SocketBase.CloseReason value)
{
Console.WriteLine($"客户端 {session.Config.Ip}:{session.Config.Port} 断开,原因:{value.ToString()}");
} private static void Server_NewSessionConnected(BinarySession session)
{
Console.WriteLine($"客户端 {session.Config.Ip}:{session.Config.Port} 已连接");
}
}
}

Main函数调用

  8.运行

  

SuperSocket框架中BinaryRequestInfo协议的使用的更多相关文章

  1. Web API应用架构在Winform混合框架中的应用(1)

    在<Web API应用架构设计分析(1)>和<Web API应用架构设计分析(2)>中对WebAPI的架构进行了一定的剖析,在当今移动优先的口号下,传统平台都纷纷开发了属于自己 ...

  2. IOS(SystemConfiguration)框架中关于测试连接网络状态相关方法

    1. 在SystemConfiguration.famework中提供和联网相关的function, 可用来检查网络连接状态. 2. SC(SystemConfiguration)框架中关于测试连接网 ...

  3. 浅析Thinkphp框架中运用phprpc扩展模式

    浅析Thinkphp框架中应用phprpc扩展模式 这次的项目舍弃了原来使用Axis2做web服务端的 方案,改用phprpc实现,其一是服务端的thinkphp已集成有该模式接口,其二是phprpc ...

  4. iOS开发概述UIkit动力学,讲述UIKit的Dynamic特性,UIkit动力学是UIkit框架中模拟真实世界的一些特性。

    转发:http://my.oschina.net/u/1378445/blog/335014 iOS UIKit动力学 Dynamics UIAttachmentBehavior 实现iMessage ...

  5. Loadrunner11中webservice协议脚本总结

    Loadrunner11中webservice协议脚本总结 简介     webservices协议是建立可交互操作的分布式应用程序的新平台,它通过一系列的标准和协议来保证程序之间的动态连接,其中最基 ...

  6. 关于HttpSession 和 Hibernate框架中 session异同点的简单解析

    快速理解: HttpSession中的session是一个容器用来盛基于会话机制的信息. 比喻:我把钱放进银行的保险柜里. 解析:我的钱就是我的信息,ID等 银行的保险柜就是session容器. Hi ...

  7. SSM框架中的前后端分离

    认识前后端分离 在传统的web应用开发中,大多数的程序员会将浏览器作为前后端的分界线.将浏览器中为用户进行页面展示的部分称之为前端,而将运行在服务器,为前端提供业务逻辑和数据准备的所有代码统称为后端. ...

  8. maven springMVC SSM框架中 出现的406 (Not Acceptable)

    首先,需要清楚,http state 406代表什么意思: 406是HTTP协议状态码的一种,表示无法使用请求的特性来响应请求的网页.一般指客户端浏览器不接受所请求页面的MIME类型. 出现这样的错误 ...

  9. 无线局域网中RADIUS协议原理与实现

    转载自:http://blog.csdn.net/jinhill/article/details/5901042 摘要  RADIUS协议是一个被广泛应用于网络认证.授权和计费的协议.本文在介绍了RA ...

随机推荐

  1. Centos安装ntfs

    ntfs优盘插在Linux上是无法直接使用的,需要安装ntfs插件才可使用 centos上安装ntfs-3g 下载ntfs-3g安装包,上传至需要安装的服务器并解压 cd 进入ntfs-3g目录,依次 ...

  2. Codeforces Round #260 (Div. 2) A , B , C 标记,找规律 , dp

    A. Laptops time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  3. CSS: iPhone Custom CSS

    1. [代码][CSS]代码 <style type="text/css" media="screen"> /* iPhone 4@media on ...

  4. 分享知识-快乐自己:JS 检查元素是否含有某种css样式

    第一种 原生 JS: ************************************************************* 结构部分: <div> <p> ...

  5. Python—numpy.bincount()

    1.它大致说bin的数量比x中的最大值大1,每个bin给出了它的索引值在x中出现的次数.下面,我举个例子让大家更好的理解一下: # 我们可以看到x中最大的数为7,因此bin的数量为8,那么它的索引值为 ...

  6. 洛谷p3369 treap

    这是一个treap裸题,还可以用splay,替罪羊树,线段树等等写 treap是树和堆结合,可以方便的在O(log(n))期望时间内进行以下操作,因此treap又叫做名次树 插入x数 删除x数(若有多 ...

  7. 浏览器对应的selenium版本问题

    在selenium+python环境下查看selenium版本方法: cmd下输入:pip show selenium 卸载selenium版本: cmd下输入:pip uninstall selen ...

  8. MYSQL中获取得最后一条记录的语句

    方法1:select max(id) from tablename 方法2:select last_insert_id(); 在MySQL中,使用auto_increment类型的id字段作为表的主键 ...

  9. web网页打印的方法

    WebBrowser.ExecWB的完整说明 个人感觉的:致命缺点-----------------仅仅支持ie浏览器 document.all.WebBrowser.ExecWB WebBrowse ...

  10. [转]Google开源Leak Finder—用于检测内存泄漏的JavaScript工具-----可惜,暂时打不开google的网站,下载不了

    近日,Google开源了Leak Finder,这款工具可以查看JavaScript应用的堆,进而发现内存泄漏. 作为一门垃圾收集语言,JavaScript并不会出现常见的内存泄露情况,特别是像C++ ...