NegotiateStream::Read Method (array<Byte>^, Int32, Int32)

 

Reads data from this stream and stores it in the specified array.

Namespace:   System.Net.Security
Assembly:  System (in System.dll)

public:
virtual int Read(
	array<unsigned char>^ buffer,
	int offset,
	int count
) override

Parameters

buffer
Type: array<System::Byte>^

A Byte array that receives the bytes read from the stream.

offset
Type: System::Int32

A Int32 containing the zero-based location in buffer at which to begin storing the data read from this stream.

count
Type: System::Int32

A Int32 containing the maximum number of bytes to read from the stream.

Return Value

Type: System::Int32

A Int32 value that specifies the number of bytes read from the underlying stream. When there is no more data to be read, returns 0.

Exception Condition
IOException

The read operation failed.

InvalidOperationException

Authentication has not occurred.

NotSupportedException

A Read operation is already in progress.

The method reads a maximum of count bytes from the current stream and stores them in buffer beginning at offset.

You cannot call this method until you have successfully authenticated. To authenticate, call one of the AuthenticateAsClient, BeginAuthenticateAsClient, AuthenticateAsServer, or BeginAuthenticateAsServer methods.

To perform this operation asynchronously, use the BeginRead method.

The following code example demonstrates reading from a 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();
}


.NET Framework
Available since 2.0
Return to top
Show: