有没有办法仅在有内容可供读取时才在 C# 中轮询套接字?
我想知道当只有一个条件(数据可供读取)满足时,是否有一种方法可以在 c# 中轮询套接字,我知道 socket.Poll 方法,但是这个如果指定的 3 个条件中的任意数量返回 true(如此处指定),则可以返回 true:MSDN:Socket.Poll
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用套接字属性可用。 它返回有多少数据可供读取。
You could use Socket property Available. It returns how much data is available to read.
在 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.
您可以在底层句柄上使用 select() 系统调用。
You could use the select() system call on the underlying handle.
您可以使用 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.
如果 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.
根据 MSDN 文档,有 3 个原因导致
返回 true
轮询(微秒,SelectMode.SelectRead);
Listen()
并且连接处于挂起状态让我们看看是否可以区分它们:
Listen()
之前是否已被调用,因此如果没有调用过,则无需考虑该原因。Poll()
返回后立即检查套接字的状态。结论:
不需要考虑
和3.可以通过每次返回true时检查套接字状态来处理。
所以我会选择(未经测试):
According to the MSDN documentation there are three causes that return true for
Poll(microSeconds, SelectMode.SelectRead);
Listen()
has been called and a connection is pendingLet's see if we can discriminate them:
Listen()
has been called before, so you don't need to consider that cause if you have not.Poll()
returned.Conclusion:
needs not to be considered
and 3. can be handled by checking the socket state each time true is returned.
So I would go for (untested):