共用方式為


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並從 開始offset儲存它們buffer

在成功驗證之前,您無法呼叫這個方法。 若要驗證,請呼叫其中 AuthenticateAsClient一個 、 AuthenticateAsClientAsyncBeginAuthenticateAsClientAuthenticateAsServerAuthenticateAsServerAsync、 或 BeginAuthenticateAsServer 方法。

若要以異步方式執行這項作業,請使用 ReadAsync 方法。

適用於