SuperSocket.ClientEngine介绍
项目地址:https://github.com/kerryjiang/SuperSocket.ClientEngine
其中需要引入的SuperSocket.ProtoBase项目:SuperSocket.ProtoBase是流数据的一个协议分析库。
地址:https://github.com/kerryjiang/SuperSocket.ProtoBase
用法:
1、根据你的网络协议创建ReceiveFilter的实现。
SuperSocket.ClientEngine提供了一些有用的基本ReceiveFilter类(在命名空间“SuperSocket.ProtoBase”下),以帮助您简化协议分析:
- TerminatorReceiveFilter ,结束符协议
- CountSpliterReceiveFilter ,固定数量分隔符协议
- FixedSizeReceiveFilter, 固定请求大小的协议
- BeginEndMarkReceiveFilter,带起止符的协议
- FixedHeaderReceiveFilter, 头部格式固定并且包含内容长度的协议
基于SuperSocket.ClientEngine提供的基本接收过滤器,您应该根据你自己协议的详细信息来设计您自己的ReceiveFilter:
class MyReceiveFilter : TerminatorReceiveFilter<StringPackageInfo> { public MyReceiveFilter() : base(Encoding.ASCII.GetBytes("||")) // two vertical bars as package terminator { } // other code you need implement according yoru protocol details }
2、创建EasyClient实例,并使用前一步中创建的ReceiveFilter来初始化它。
var client = new EasyClient(); // Initialize the client with the receive filter and request handler client.Initialize(new MyReceiveFilter(), (request) => { // handle the received request Console.WriteLine(request.Key); });
3、建立连接,然后开始通信。
// Connect to the server var connected = await client.ConnectAsync(new IPEndPoint(IPAddress.Parse("192.168.10.11"), 25)); if (connected) { // Send data to the server client.Send(Encoding.ASCII.GetBytes("LOGIN kerry")); }
posted on 2018-08-03 16:03 springsnow 阅读(9650) 评论(2) 编辑 收藏 举报