我正在将 C# 代码从使用 NetworkStream 迁移到 SSLStream,但是在使用 stream.DataAvailable 的地方出现错误:
错误 1“System.Net.Security.SslStream”不包含“DataAvailable”的定义,并且找不到接受“System.Net.Security.SslStream”类型的第一个参数的扩展方法“DataAvailable”(您是否缺少使用指令还是程序集引用?)
现在我的本地 MSDN 副本不包括 DataAvailable 作为 SslStream 的成员,但是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
--EDIT 我刚刚意识到我链接到嵌入式SDK,这不是嵌入式系统,那么我如何查看普通.net SDK中的数据是否可用?