NegotiateStream.Read(Byte[], Int32, Int32) Método

Definição

Lê os dados desse fluxo e o armazena na matriz especificada.

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

Parâmetros

buffer
Byte[]

Uma matriz Byte que recebe os bytes lidos do fluxo.

offset
Int32

Um Int32 contendo a localização baseada em zero no buffer em que será iniciado o armazenamento dos dados lidos desse fluxo.

count
Int32

Um Int32 contendo o número máximo de bytes a serem lidos do fluxo.

Retornos

Int32

Um valor Int32 que especifica o número de bytes lidos do fluxo subjacente. Quando não há mais dados a serem lidos, retorna 0.

Exceções

Falha na operação de leitura.

A autenticação não ocorreu.

Uma operação Read(Byte[], Int32, Int32) já está em andamento.

Exemplos

O exemplo de código a seguir demonstra a leitura de um 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();
}

Comentários

O método lê um máximo de count bytes do fluxo atual e os armazena no buffer início em offset.

Você não pode chamar esse método até ter se autenticado com êxito. Para autenticar, chame um dos AuthenticateAsClientmétodos , AuthenticateAsClientAsync, BeginAuthenticateAsClient, AuthenticateAsServerou AuthenticateAsServerAsyncBeginAuthenticateAsServer

Para executar essa operação de forma assíncrona, use o ReadAsync método.

Aplica-se a