NegotiateStream.Read(Byte[], Int32, Int32) Metoda

Definicja

Odczytuje dane z tego strumienia i przechowuje je w określonej tablicy.

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

Parametry

buffer
Byte[]

Tablica Byte , która odbiera bajty odczytane ze strumienia.

offset
Int32

Lokalizacja Int32 zawierająca lokalizację opartą na zera, w buffer której ma rozpocząć przechowywanie danych odczytanych ze strumienia.

count
Int32

Wartość Int32 zawierająca maksymalną liczbę bajtów do odczytu ze strumienia.

Zwraca

Wartość Int32 określająca liczbę bajtów odczytanych ze strumienia bazowego. Jeśli nie ma więcej danych do odczytu, zwraca wartość 0.

Wyjątki

Operacja odczytu nie powiodła się.

Uwierzytelnianie nie wystąpiło.

Przykłady

Poniższy przykład kodu przedstawia odczyt z elementu 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();
}

Uwagi

Metoda odczytuje maksymalnie bajty count z bieżącego strumienia i przechowuje je na buffer początku na offset.

Nie można wywołać tej metody do momentu pomyślnego uwierzytelnienia. Aby przeprowadzić uwierzytelnianie, wywołaj jedną z AuthenticateAsClientmetod , , AuthenticateAsClientAsyncAuthenticateAsServerBeginAuthenticateAsClient, AuthenticateAsServerAsynclub .BeginAuthenticateAsServer

Aby wykonać tę operację asynchronicznie, użyj ReadAsync metody .

Dotyczy