NegotiateStream.Read(Byte[], Int32, Int32) 方法

定义

读取此流中的数据并将其存储在指定的数组中。

public:
 override int Read(cli::array <System::Byte> ^ buffer, int offset, int count);
public override int Read (byte[] buffer, int offset, int count);
override this.Read : byte[] * int * int -> int
Public Overrides Function Read (buffer As Byte(), offset As Integer, count As Integer) As Integer

参数

buffer
Byte[]

一个 Byte 数组,该数组接收从流中读取的字节。

offset
Int32

Int32,其中包含 buffer 中从零开始的位置,从此处开始存储从流中读取的数据。

count
Int32

Int32,包含要从流中读取的最大字节数。

返回

Int32 值,该值指定从基础流中读取的字节数。 如果再也没有要读取的数据,将返回 0。

例外

读操作失败。

未进行身份验证。

示例

下面的代码示例演示如何从 NegotiateStream读取 。

static void AuthenticateClient( TcpClient^ clientRequest )
{
   NetworkStream^ stream = clientRequest->GetStream();
   
   // Create the NegotiateStream.
   NegotiateStream^ authStream = gcnew NegotiateStream( stream,false );
   
   // Perform the server side of the authentication.
   authStream->AuthenticateAsServer();
   
   // Display properties of the authenticated client.
   IIdentity^ id = authStream->RemoteIdentity;
   Console::WriteLine( L"{0} was authenticated using {1}.", id->Name, id->AuthenticationType );
   
   // Read a message from the client.
   array<Byte>^buffer = gcnew array<Byte>(2048);
   int charLength = authStream->Read( buffer, 0, buffer->Length );
   String^ messageData = gcnew String( Encoding::UTF8->GetChars( buffer, 0, buffer->Length ) );
   Console::WriteLine( L"READ {0}", messageData );
   
   // Finished with the current client.
   authStream->Close();
   
   // Close the client connection.
   clientRequest->Close();
}


public static void AuthenticateClient(TcpClient clientRequest)
{
    NetworkStream stream = clientRequest.GetStream();
    // Create the NegotiateStream.
    NegotiateStream authStream = new NegotiateStream(stream, false);
    // Perform the server side of the authentication.
    authStream.AuthenticateAsServer();
    // Display properties of the authenticated client.
    IIdentity id = authStream.RemoteIdentity;
    Console.WriteLine("{0} was authenticated using {1}.",
        id.Name,
        id.AuthenticationType
        );
    // Read a message from the client.
    byte [] buffer = new byte[2048];
    int charLength = authStream.Read(buffer, 0, buffer.Length);
    string messageData = new String(Encoding.UTF8.GetChars(buffer, 0, buffer.Length));

    Console.WriteLine("READ {0}", messageData);
    // Finished with the current client.
    authStream.Close();
    // Close the client connection.
    clientRequest.Close();
}

注解

方法从当前流中读取最大字节数 count ,并将它们 buffer 存储在 从 offset开始的 中。

在成功进行身份验证之前,无法调用此方法。 若要进行身份验证,请调用 、、AuthenticateAsClientAsyncAuthenticateAsServerAsyncBeginAuthenticateAsClientAuthenticateAsServer、 或 BeginAuthenticateAsServer 方法之一。AuthenticateAsClient

若要异步执行此操作,请使用 ReadAsync 方法。

适用于