加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 事件驱动的TCP客户端

发布时间:2020-12-15 07:41:09 所属栏目:百科 来源:网络整理
导读:我想要一个事件驱动的 windows,c#,tcp客户端. 当读缓冲区中至少有35个字节时,我希望调用一个处理程序来读取这35个字节,从该“数据包”中获取一个长度值,然后对该第二个数据长度进行阻塞读取. 解决方法 有一个相对较新的项目基本上提供了这个: https://githu
我想要一个事件驱动的 windows,c#,tcp客户端.

当读缓冲区中至少有35个字节时,我希望调用一个处理程序来读取这35个字节,从该“数据包”中获取一个长度值,然后对该第二个数据长度进行阻塞读取.

解决方法

有一个相对较新的项目基本上提供了这个: https://github.com/clariuslabs/reactivesockets

从他们的页面:

Implements a very easy to use sockets API based on IObservable. It allows very simple protocol implementations such as:

var client = new ReactiveClient("127.0.0.1",1055);

// The parsing of messages is done in a simple Rx query over the receiver observable
// Note this protocol has a fixed header part containing the payload message length
// And the message payload itself. Bytes are consumed from the client.Receiver 
// automatically so its behavior is intuitive.
IObservable<string> messages = from header in client.Receiver.Buffer(4)
                               let length = BitConverter.ToInt32(header.ToArray(),0)
                               let body = client.Receiver.Take(length)
                               select Encoding.UTF8.GetString(body.ToEnumerable().ToArray());

// Finally,we subscribe on a background thread to process messages when they are available
messages.SubscribeOn(TaskPoolScheduler.Default).Subscribe(message => Console.WriteLine(message));
client.ConnectAsync().Wait();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读