一、开发环境

  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. Delphi 中关闭指定进程的方法

    Uses Windows, SysUtils, Tlhelp32 ; Function KillTask( ExeFileName: String ): Integer ; //关闭进程 Functi ...

  2. Sobel导数

    Sobel 导数 目标 本文档尝试解答如下问题: 如何使用OpenCV函数 Sobel 对图像求导. 如何使用OpenCV函数 Scharr 更准确地计算  核的导数. 原理 Note 以下内容来自于 ...

  3. UniDAC 安装教程

    翻译: 1.解压后把UniDAC文件夹直接复制到你专门用来存放第三方控件的地方(这一步根据自己的喜好,可以跳过这一步)2.在UniDAC\Source\Delphi21文件夹中找到Make.bat文件 ...

  4. css中单位px和em,rem的区别

    PX:PX实际上就是像素,用PX设置字体大小时,比较稳定和精确.但是这种方法存在一个问题,当用户在浏览器中浏览我们制作的Web页面时,如果改变了浏览器的缩放,这时会使用我们的Web页面布局被打破.这样 ...

  5. codeforces 633B B. A Trivial Problem(数论)

    B. A Trivial Problem time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. Java中数学计算的相关方法

    1:Math类 2.BigInteger类  3.BigDecimal类 BigInteger bi = new BigInteger("12433241123"); BigDec ...

  7. PS 滤镜——波浪 wave

    %%% Wave %%% 波浪效果 clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Alg ...

  8. Python 爬虫闯关(第一关)

    在学习爬虫时,遇到了一个有意思的网站,这个网站设置了几个关卡,需要经过爬虫进行闯关,随着关卡的网后,难度不断增加,在闯关的过程中需要学习不同的知识,你的爬虫水平也自然随之提高. 今天我们先来第一关,访 ...

  9. 洛谷 P1379 八数码难题

    题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出一种初始布局(初始状态)和目标布局(为了 ...

  10. Access中一句查询代码实现Excel数据导入导出

    摘 要:用一句查询代码,写到vba中实现Excel数据导入导出,也可把引号中的SQL语句直接放到查询分析器中执行正 文: 导入数据(导入数据时第一行必须是字段名): DoCmd.RunSQL &quo ...