NegotiateStream.Read(Byte[], Int32, Int32) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從這個資料流中讀取資料並將其儲存於指定的陣列中。
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
參數
傳回
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一個 、 AuthenticateAsClientAsync、 BeginAuthenticateAsClient、 AuthenticateAsServer、 AuthenticateAsServerAsync、 或 BeginAuthenticateAsServer 方法。
若要以異步方式執行這項作業,請使用 ReadAsync 方法。