有没有办法仅在有内容可供读取时才在 C# 中轮询套接字?

发布于 2024-07-30 07:37:58 字数 257 浏览 5 评论 0 原文

我想知道当只有一个条件(数据可供读取)满足时,是否有一种方法可以在 c# 中轮询套接字,我知道 socket.Poll 方法,但是这个如果指定的 3 个条件中的任意数量返回 true(如此处指定),则可以返回 true:MSDN:Socket.Poll

I'm wondering whether there is a way to poll a socket in c# when only one of the conditions (data is available for reading) is satisfied, I'm aware of the socket.Poll method but this can return true if any number of the 3 conditions specified returns true as specified here: MSDN: Socket.Poll

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

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

发布评论

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

评论(6

睫毛上残留的泪 2024-08-06 07:37:59

您可以使用套接字属性可用。 它返回有多少数据可供读取。

You could use Socket property Available. It returns how much data is available to read.

时间海 2024-08-06 07:37:59

在 NetworkStream 类中找到了一些东西。 属性 NetworkStream.DataAvailable 返回 true数据是否可供读取。 返回处理 TcpListener 和 TcpClient 的网络流对象。 这是比套接字高一级的抽象级别。

我找不到从 Socket 到 NetworkStream 的方法。 NetworkStream 使用套接字,并且是套接字的流表示形式。 但我不知道网络流在套接字上做什么。

Found something in class NetworkStream. Property NetworkStream.DataAvailable returns true if data is available for reading. Object of networkstream is returned dealing with TcpListener and TcpClient. This is one abstraction level higher than socket.

I found no way to come from Socket to NetworkStream. A NetworkStream is using a socket and is the stream representation of a socket. But I dont know what the networkstream is doing there with the socket.

一抹微笑 2024-08-06 07:37:59

您可以在底层句柄上使用 select() 系统调用。

You could use the select() system call on the underlying handle.

无力看清 2024-08-06 07:37:59

您可以使用 Select() 方法代替 Poll()。
实际上,当使用 ILSpy(反射器工具)查看 Socket.Poll 时,内部代码正在调用套接字上的 select 。

此外,在紧密循环中调用 Poll() 将增加内存分配,因为它在每次调用时都会创建一个新的 IntPtr[]。
调用 Select() 可以让您重用数组,而不是在后台分配新数组。

You can use Select() method instead of Poll().
Actually when looking into Socket.Poll with ILSpy (reflector tool) the internal code is calling select on the socket.

In addition, calling Poll() in a tight loop will increase memory allocation as it does a new IntPtr[] on each call.
Calling Select() lets you reuse the arrays instead of allocating new ones under the hood.

〃温暖了心ぐ 2024-08-06 07:37:59

如果 Listen 已被调用且连接处于挂起状态,则为 true;
-或者-
如果数据可供读取,则为 true;
-或者-
如果连接已关闭、重置或终止,则为 true;
否则,返回 false。

我知道您想检查第二个选项是否是返回 true 的选项? 检查 Poll 是否返回 true 后,您可以检查连接是否打开,这意味着; 未连接、关闭、重置或终止。

如果它是打开的,那么它是第二个选项返回true。

true if Listen has been called and a connection is pending;
-or-
true if data is available for reading;
-or-
true if the connection has been closed, reset, or terminated;
otherwise, returns false.

I understand that you want to check if the second option is the one returning true? After checking if the Poll returns true, you can check if the connection is open, that means; not connection, closed, reset or terminated.

If it is open, then it's the second option return true.

脱离于你 2024-08-06 07:37:58

根据 MSDN 文档,有 3 个原因导致

返回 true
轮询(微秒,SelectMode.SelectRead);

  1. 如果已调用 Listen() 并且连接处于挂起状态
  2. 如果数据可供读取
  3. 如果连接已关闭、重置或终止

让我们看看是否可以区分它们:

  1. 您总是知道 Listen() 之前是否已被调用,因此如果没有调用过,则无需考虑该原因。
  2. 好吧,你就这么做吧。
  3. 意味着你不能停留在 Poll() 调用中,需要找出到底发生了什么。 一种选择是在 Poll() 返回后立即检查套接字的状态。

结论:

  1. 不需要考虑

  2. 和3.可以通过每次返回true时检查套接字状态来处理。

所以我会选择(未经测试):

if (s.Poll(microSeconds, SelectMode.SelectRead)))
{
  if (!s.Connected)
    // Something bad has happened, shut down
  else
    // There is data waiting to be read"
}

According to the MSDN documentation there are three causes that return true for


Poll(microSeconds, SelectMode.SelectRead);

  1. if Listen() has been called and a connection is pending
  2. If data is available for reading
  3. If the connection has been closed, reset, or terminated

Let's see if we can discriminate them:

  1. You always know if Listen() has been called before, so you don't need to consider that cause if you have not.
  2. Ok, you go for that.
  3. Means that you can not stay in the Poll() call and need to find out what really happened. One option is to check the socket's state immediately after Poll() returned.

Conclusion:

  1. needs not to be considered

  2. and 3. can be handled by checking the socket state each time true is returned.

So I would go for (untested):

if (s.Poll(microSeconds, SelectMode.SelectRead)))
{
  if (!s.Connected)
    // Something bad has happened, shut down
  else
    // There is data waiting to be read"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文