NetworkStream,是否有类似于 SerialPort 的 DataReceived 的东西? (C#)

发布于 2024-07-30 11:47:41 字数 608 浏览 4 评论 0原文

好吧,所以我有点困惑为什么我在任何地方都找不到这个,或者如果它不存在那么为什么微软没有实现它?

这是我的场景,我有一个 NetworkStream,它有一个可爱的小布尔值,名为 DataAvailable,我需要的是一个事件,它会跳出来并说“嘿,有可用的数据!” (因为我很懒,我宁愿被告知有可用数据,而不是一遍又一遍地问“好吧,有可用数据吗?”,直到我得到答复“实际上,这次有”)。

类似 SerialPort 的东西(它有一个很好的事件(DataReceived),它友好地通知我正在从端口接收数据)会很好。 但我使用带有 NetworkStream 的套接字。

如果有明显我遗漏的东西,请为我指出正确的方向,但如果没有,这是否意味着我将不得不在 DataAvailable 属性上使用一些数据绑定,并且当它设置为 true 时,调用我的自己的“自制”活动/功能? 如果这是这样的话,你能给我一个小例子来让事情顺利进行吗?

编辑
我的完美答案是有人过来向我解释如何找到/创建与 SerialPort 一起使用的 DataReceived 事件极其相似的东西,但针对通过 NetworkStream 进行流传输的 Socket 实现!

再次提前致谢,不胜感激。

Ok, so I'm a little confused as to why I can't find this anywhere, or if it doesn't exist then why have Microsoft not implemented it?

So here's my scenario, I have a NetworkStream, which has a lovely little boolean called DataAvailable, and what I need is an event, that jumps out and says "Hey, there's data available for you!" (because I'm lazy and I'd rather be told that there's data available than to keep asking "Alright, is there any data available?" over and over again until I get the response "Actually, this time there is").

Something similar a SerialPort (which has a nice event (DataReceived), that kindly informs me that data is being received from the port) would have been nice. But I'm using a Socket with a NetworkStream.

Point me in the correct direction if there's something blatently obvious that I'm missing, but if not, does this mean I am going to have to use some Data Binding on the DataAvailable property, and when it is set to true, to call my own 'home made' event/function? If this is going to be the way could you please give me a small example to get the ball rolling?

Edit
My perfect answer would be for someone to come along and explain to me how I can find/create something extremely similar to the DataReceived Event used with a SerialPort, but implemented for a Socket that is streaming via a NetworkStream!

Thanks in advance again, appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小清晰的声音 2024-08-06 11:47:41

只是为了澄清 Thomas 的答案,并为任何不熟悉 BeginRead 方法并希望了解此问题答案的人提供解释,您可以调用:

AsyncCallBack MyCallBack = new AsynCallBack(DataReceived);
networkStream.BeginRead(buffer, offset, size, MyCallBack, MyObject);

然后创建 DataReceived 函数:

private void DataReceived(IAsynResult result)
  {
  //call receive functionality
  }

这将调用 DataReceived ,与SerialPort.DataReceived 事件有效。

Just to clarify Thomas' answer, with an explanation for anyone who isn't familliar with the BeginRead method and wants to understand the answer to this question, you can call:

AsyncCallBack MyCallBack = new AsynCallBack(DataReceived);
networkStream.BeginRead(buffer, offset, size, MyCallBack, MyObject);

then create the DataReceived function:

private void DataReceived(IAsynResult result)
  {
  //call receive functionality
  }

This will call DataReceived extremely similar to how the SerialPort.DataReceived event works.

过期以后 2024-08-06 11:47:41

NetworkStream 类中没有事件 (请参阅 MSDN)。 NetworkStream继承自Stream,因此它遵循流模型,不基于事件。 如果需要异步接收数据,请使用BeginRead方法

There are no events in the NetworkStream class (see MSDN). NetworkStream inherits from Stream, therefore it follows the stream model, which is not based on events. If you need to receive data asynchronously, use the BeginRead method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文