我正在将 C# 代码从使用 NetworkStream 迁移到 SSLStream,但是在我使用 stream.DataAvailable 的地方出现错误:
Error 1 'System.Net.Security.SslStream' does not contain a definition for 'DataAvailable' and no extension method 'DataAvailable' accepting a first argument of type 'System.Net.Security.SslStream' could be found (are you missing a using directive or an assembly reference?)
现在我的本地 MSDN 副本不包括作为 SslStream 成员的 DataAvailable http://msdn.microsoft.com/en-us/library/dd170317.aspx说它确实有成员 DataAvailable。 这是我的代码的副本。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.IO;
namespace Node
{
public static class SSLCommunicator
{
static TcpClient client = null;
static SslStream stream = null;
static List<byte> networkStreamInput = new List<byte>();
public static void connect(string server, Int32 port)
{
try
{
client = new TcpClient(server, port);
stream = new SslStream(client.GetStream(),false);
...
...
...
public static List<DataBlock> getServerInput()
{
List<DataBlock> ret = new List<DataBlock>();
try
{
//check to see if stream is readable.
if (stream.CanRead)
{
//Check to see if there is data available.
if (stream.DataAvailable)
{
byte[] readBuffer = new byte[1024];
int numberOfBytesRead = 0;
//while data is available buffer the data.
do
{
numberOfBytesRead = stream.Read(readBuffer, 0, readBuffer.Length);
byte[] tmp = new byte[numberOfBytesRead];
Array.Copy(readBuffer, tmp, numberOfBytesRead);
networkStreamInput.AddRange(tmp);
} while (stream.DataAvailable);
...
此外,如果您有更好的方法将我的流输出放入托管数组(代码稍后会对其进行一些解析),我会很乐意提供帮助。我正在使用 Visual Studio 2008
--编辑 我刚刚意识到我链接到嵌入式 SDK,这不是嵌入式系统,那么我如何查看普通 .net SDK 中的数据是否可用?
最佳答案
您正在查看的页面适用于 .NET Micro Framework。
根据 this page for .Net 2.0和 this page for .Net 3.5 , SSLStream 上没有 DataAvailable 属性。
编辑:您不能只调用 Read() 并查看是否返回任何内容吗?我认为这不会阻塞。
关于c# - SslStream.DataAvailable 不是有效函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/691085/