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
,并将它们 buffer
存储在 从 offset
开始的 中。
在成功进行身份验证之前,无法调用此方法。 若要进行身份验证,请调用 、、AuthenticateAsClientAsync、AuthenticateAsServerAsyncBeginAuthenticateAsClientAuthenticateAsServer、 或 BeginAuthenticateAsServer 方法之一。AuthenticateAsClient
若要异步执行此操作,请使用 ReadAsync 方法。