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.

Исключения

Сбой операции чтения.

Проверка подлинности не выполнялась.

Операция Read(Byte[], Int32, Int32) уже выполняется в настоящее время.

Примеры

В следующем примере кода демонстрируется чтение из 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.

Вы не сможете вызвать этот метод, пока не пройдете проверку подлинности. Для проверки подлинности AuthenticateAsClientвызовите один из методов , BeginAuthenticateAsClientAuthenticateAsClientAsync, AuthenticateAsServer, , AuthenticateAsServerAsyncили BeginAuthenticateAsServer .

Чтобы выполнить эту операцию асинхронно, используйте ReadAsync метод .

Применяется к