zoukankan      html  css  js  c++  java
  • 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类

      

      
    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, 0, 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(7788);//设置端口号
                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.运行

      

    鸑鷟虽孤,不匹鹜雏。
  • 相关阅读:
    新院址运行统计
    游标使用之四
    游标使用之三
    css基础知识
    javascript基础知识
    [每日一题2020.06.20]BFS
    白嫖一个免费域名并解析到博客园
    [每日一题2020.06.19]leetcode #84 #121 单调栈
    操作系统---文件管理
    [每日一题2020.06.18]leetcode #3 hash_map实现滑动窗口
  • 原文地址:https://www.cnblogs.com/zzuwangzhen/p/9564370.html
Copyright © 2011-2022 走看看