一、开发环境

  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. Struts2 内核之我见

    Struts2 内核之我见 完整分析 Struts2 内核中文文档 本文首先探讨了 Struts2 核心控制器的源码,以帮助解读 Struts2 的工作流程.接着讲解相关外围类.最后对 Struts ...

  2. JSP<jsp:forward>与<%@ include%>

    JSP<jsp:forward>与<%@ include%><jsp:include> <jsp:forward file="forwardTo_p ...

  3. appium-环境搭建(三)

    appium步骤:基本环境1.由于操作手机端操作,需要模拟器或者真机 itools模拟器,真机2.appium操作app,需要知道操作的app是什么?需要知道这个app包名 1.问开发 2.利用adt ...

  4. JavaUtil_06_HttpUtil_使用httpclient实现

    一.简介 使用 appache 的 httpclient 来实现的 二.源码 package com.ray.weixin.gz.util; import java.io.File; import j ...

  5. Oracle_Exception_01_The Network Adapter could not establish the connection

    1.IP错误或端口错误. 在设置URL时错误,例如 url="jdbc:oracle:thin:@192.168.1.11:1521:mas" 数据库服务器不正确:ping 服务器 ...

  6. NserviceBus过期处理

    NserviceBus不过期,修改注册表 HKEY_CURRENT_USER\Software\NServiceBus\3.3下的项TrailDate到2099-08-02

  7. CyclicBarrier与CountDownLatch的区别

    import java.util.concurrent.CountDownLatch; /** * 作用于单个线程或几个线程,,在其他线程执行完之前,一直等待(await)知道countDown为零 ...

  8. 【leetcode刷题笔记】Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  9. POJ1379:Run Away

    我对模拟退火的理解:https://www.cnblogs.com/AKMer/p/9580982.html 我对爬山的理解:https://www.cnblogs.com/AKMer/p/95552 ...

  10. python中http请求中添加cookie支持

    python3中构造http的Request需要用到urllib.request. 有时会用到cookie. 比如在访问网站首页得到cookie,通过下面代码添加cookie:      #insta ...