1、通过之前的学习,我们知道在SuperSocket中,客户端向服务器发送数据时需要以回车换行符“\r\n”结尾服务端才能够识别。这是因为SuperSocket的默认协议CommandLineProtocol(命令行协议)要求所致。SuperSocket还有以下常用的协议(我按照翻译的……)。
1.1、CommandLineProtocol:命令行协议,要求以回车换行符“\r\n”结尾。
1.2、TerminatorReceiveFilter:结束符协议。
1.3、CountSpliterReceiveFilter:固定数量分隔符协议。
1.4、FixedSizeReceiveFilter:固定请求大小协议。
1.5、BeginEndMarkReceiveFilter:起止标记协议。
1.6、FixedHeaderReceiveFilter:固定头部协议。
2、在之前的学习中,我们通过使用StringRequestInfo来解析客户端发送的数据。StringRequestIfo有三个属性,
2.1、Key:协议中的命令字段,是发送数据用空格分隔的第一个字符串,例如我们之前使用的SocketCommand1.cs,客服端想要调用SocketCommand1.cs中的ExecuteCommand方法需要先发送"SocketCommand1"字符串作为命令。用空格与后面的内容隔开。
2.2、Body:命令的参数部分,以SocketCommand1举例,客户端发送"SocketCommand1 123 321",那么Body部分就是"123 321"。
2.3、Parameters:命令的参数组,以SocketCommand1举例,客户端发送"SocketCommand1 123 321",那么Parameters部分就是["123","321"]。
2.4、客户发送的数据key与参数直接默认是以空格隔开。
3、除了StringBinaryRequestInfo以外,SuperSocket还支持BinaryRequestInfo用于二进制传输。
4、客户端向服务器传输的命令和参数可以使用其它分隔符进行分隔,如下面代码使用“,”和“:”进行分隔。
        public SocketServer() : base(
            new CommandLineReceiveFilterFactory(Encoding.Default, new BasicRequestInfoParser(",", ":")))
        {
        }完整代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketBase.Protocol;
namespace ConsoleApp1.Config
{
    /// <summary>
    /// 重写AppServer类
    /// </summary>
    public class SocketServer:AppServer<SocketSession>
    {
        public SocketServer() : base(
            new CommandLineReceiveFilterFactory(Encoding.Default, new BasicRequestInfoParser(",", ":")))
        {
        }
        /// <summary>
        /// 重写启动SocketServer
        /// </summary>
        /// <param name="rootConfig"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        protected override bool Setup(IRootConfig rootConfig, IServerConfig config)
        {
            Console.WriteLine("启动SocketServer");
            return base.Setup(rootConfig, config);
        }
        /// <summary>
        /// 重写服务已启动
        /// </summary>
        protected override void OnStarted()
        {
            Console.WriteLine("服务已开始");
            base.OnStarted();
        }
        /// <summary>
        /// 重写服务已停止
        /// </summary>
        protected override void OnStopped()
        {
            Console.WriteLine("服务已停止");
            base.OnStopped();
        }
        /// <summary>
        /// 重写新的连接建立
        /// </summary>
        /// <param name="session"></param>
        protected override void OnNewSessionConnected(SocketSession session)
        {
            base.OnNewSessionConnected(session);
            Console.WriteLine("新客户端接入,IP地址为:"+session.RemoteEndPoint.Address.ToString());
            session.Send("Welcome");
        }
        /// <summary>
        /// 重写客户端连接关闭
        /// </summary>
        /// <param name="session"></param>
        /// <param name="reason"></param>
        protected override void OnSessionClosed(SocketSession session, CloseReason reason)
        {
            base.OnSessionClosed(session, reason);
            Console.WriteLine("客户端断开,IP地址为:" + session.RemoteEndPoint.Address.ToString());
        }
    }
}
5、TerminatorReceiveFilter结束符协议,修改方法。
 
5.1、 新建TerminatorProtocolSession.cs。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Protocol;
namespace ConsoleApp1
{
    /// <summary>
    /// 自定义结束符类TerminatorProtocolSession
    /// </summary>
    public class TerminatorProtocolSession:AppSession<TerminatorProtocolSession>
    {
        /// <summary>
        /// 重写发送
        /// </summary>
        /// <param name="message"></param>
        public override void Send(string message)
        {
            Console.WriteLine("发送消息:" + message);
            base.Send(message);
        }
        /// <summary>
        /// 重写客户端建立连接
        /// </summary>
        protected override void OnSessionStarted()
        {
            Console.WriteLine("客户端IP地址:" + this.LocalEndPoint.Address.ToString());
            base.OnSessionStarted();
        }
        /// <summary>
        /// 重写客户端断开连接
        /// </summary>
        /// <param name="reason"></param>
        protected override void OnSessionClosed(CloseReason reason)
        {
            base.OnSessionClosed(reason);
        }
        /// <summary>
        /// 重写未知请求
        /// </summary>
        /// <param name="requestInfo"></param>
        protected override void HandleUnknownRequest(StringRequestInfo requestInfo)
        {
            Console.WriteLine("遇到位置请求Key:" + requestInfo.Key + "  Body:" + requestInfo.Body);
            base.HandleUnknownRequest(requestInfo);
        }
        /// <summary>
        /// 重写捕获异常
        /// </summary>
        /// <param name="e"></param>
        protected override void HandleException(Exception e)
        {
            Console.WriteLine("error:{0}", e.Message);
            this.Send("error:{0}", e.Message);
            base.HandleException(e);
        }
    }
}
5.2、修改SocketServer.cs代码如下。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperSocket.Common;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketBase.Protocol;
using SuperSocket.SocketEngine;
namespace ConsoleApp1.Config
{
    /// <summary>
    /// 重写AppServer类
    /// </summary>
    public class SocketServer:AppServer<TerminatorProtocolSession>
    {
        public SocketServer() : base(new TerminatorReceiveFilterFactory("###"))
        {
        }
        /// <summary>
        /// 重写启动SocketServer
        /// </summary>
        /// <param name="rootConfig"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        protected override bool Setup(IRootConfig rootConfig, IServerConfig config)
        {
            Console.WriteLine("启动SocketServer");
            return base.Setup(rootConfig, config);
        }
        /// <summary>
        /// 重写服务已启动
        /// </summary>
        protected override void OnStarted()
        {
            Console.WriteLine("服务已开始");
            base.OnStarted();
        }
        /// <summary>
        /// 重写服务已停止
        /// </summary>
        protected override void OnStopped()
        {
            Console.WriteLine("服务已停止");
            base.OnStopped();
        }
        /// <summary>
        /// 重写新的连接建立
        /// </summary>
        /// <param name="session"></param>
        protected override void OnNewSessionConnected(TerminatorProtocolSession session)
        {
            base.OnNewSessionConnected(session);
            Console.WriteLine("新客户端接入,IP地址为:" + session.RemoteEndPoint.Address.ToString());
            session.Send("Welcome");
        }
        /// <summary>
        /// 重写客户端连接关闭
        /// </summary>
        /// <param name="session"></param>
        /// <param name="reason"></param>
        protected override void OnSessionClosed(TerminatorProtocolSession session, CloseReason reason)
        {
            base.OnSessionClosed(session, reason);
            Console.WriteLine("客户端断开,IP地址为:" + session.RemoteEndPoint.Address.ToString());
        }
        / <summary>
        / 重写新的连接建立
        / </summary>
        / <param name="session"></param>
        //protected override void OnNewSessionConnected(SocketSession session)
        //{
        //    base.OnNewSessionConnected(session);
        //    Console.WriteLine("新客户端接入,IP地址为:"+session.RemoteEndPoint.Address.ToString());
        //    session.Send("Welcome");
        //}
        / <summary>
        / 重写客户端连接关闭
        / </summary>
        / <param name="session"></param>
        / <param name="reason"></param>
        //protected override void OnSessionClosed(SocketSession session, CloseReason reason)
        //{
        //    base.OnSessionClosed(session, reason);
        //    Console.WriteLine("客户端断开,IP地址为:" + session.RemoteEndPoint.Address.ToString());
        //}
    }
}
5.3、修改SocketCommand1.cs和SocketCommand2.cs代码如下。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
namespace ConsoleApp1.Config
{
    /// <summary>
    /// 自定义SocketCommand1
    /// </summary>
    public class SocketCommand1 : CommandBase<TerminatorProtocolSession, StringRequestInfo>
    {
        public override void ExecuteCommand(TerminatorProtocolSession session, StringRequestInfo requestInfo)
        {
            session.Send("1");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
namespace ConsoleApp1.Config
{
    /// <summary>
    /// 自定义Command1
    /// </summary>
    public class SocketCommand2 : CommandBase<TerminatorProtocolSession, StringRequestInfo>
    {
        public override void ExecuteCommand(TerminatorProtocolSession session, StringRequestInfo requestInfo)
        {
            session.Send(string.Format("{0} {1}","1","123"));
        }
    }
}
5.4、项目结构如下。

5.5、 App.config文件内容如下。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<configSections>
		<!--log 日志记录-->
		<section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
		<!--SocketEngine-->
		<section name="superSocket" type="SuperSocket.SocketEngine.Configuration.SocketServiceConfig, SuperSocket.SocketEngine"/>
	</configSections>
	<!--服务信息描述,在window服务模式下的名称标识-->
	<appSettings>
		<add key="ServiceName" value="ConsoleApp1"/>
		<add key="ServiceDescription" value="bjxingch"/>
	</appSettings>
	<!--SuperSocket服务配置信息 serverType是项目的服务如我自定义的Socketserver-->
	<superSocket>
		<servers>
			<server name="ConsoleApp1"
			        textEncoding="gb2312"
			        serverType="ConsoleApp1.Config.SocketServer,ConsoleApp1"
			        ip="Any"
			        port="2024"
			        maxConnectionNumber="100"/>
			<!--
				name:实例名称
				textEncoding:编码方式"gb2312","utf-8" 默认是acii
				serverType:需要注意,否则无法启动Server
				ip:监听ip
				port:端口号
				maxConnectionNumber:最大连接数
			-->
		</servers>
	</superSocket>
	<startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
</configuration>6、Demo下载路径如下。
https://download.csdn.net/download/xingchengaiwei/89316001


















![[idea/git] 如何把多模块项目提交到一个仓库](https://img-blog.csdnimg.cn/direct/8be180dd040e490194daa7d9ed10370c.png)
