1

不久前,我在 vb.net 中编写了一些基本的 Http 网络服务器。我试图避免阻塞 IO,所以基本上我为所有当前连接创建了一个轮询线程。

While True
    For Each oNetworkstream In lstNetworkstream
        If oNetworkstream.DataAvailable Then
            'Read from stream
        End If
    Next
End While

因此,每当连接有一些新数据时,我都可以读取它,否则立即检查下一个连接。

现在我正在使用 https 扩展网络服务器。因此,我使用了 .Net SslStream 类。我想应用相同的原则(一个轮询线程来读取所有流)

由于没有 .DataAvailable 属性,我尝试了宽度 .Length > 0,但这给出了 NotSupportedException(此流不支持查找操作)

Dim oSslStream As New SslStream(oStream, False)
oSslStream.AuthenticateAsServer(moCertificateKeyPair)
MsgBox(oSslStream.Length)

那么,我如何确定某个解密的流是否有可用的数据而不阻塞线程呢?

4

1 回答 1

0

Avoiding blocking is a good idea when you have many connections. But polling is not the way to solve it.

Instead, you should use async IO. Let the system notify you when data is ready.

With C# 5 you should use async/await + ReadAsync. In lower C# versions you should use Task-based IO (IOW still use ReadAsync is available). If it is not available, write your own version. If you can't do that, use BeginRead.

于 2013-08-31T14:15:23.717 回答