Export (0) Print
Expand All

SslStream.Read Method

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

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

public override int Read(
	byte[] buffer,
	int offset,
	int count
)

Parameters

buffer
Type: System.Byte[]

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

offset
Type: System.Int32

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

count
Type: System.Int32

A Int32 that contains the maximum number of bytes to read from this stream.

Return Value

Type: System.Int32
A Int32 value that specifies the number of bytes read. When there is no more data to be read, returns 0.

ExceptionCondition
ArgumentNullException

buffer is null.

ArgumentException

offset < 0.

-or-

offset > the length of buffer.

-or-

offset + count > the length of buffer.

IOException

The read operation failed. Check the inner exception, if present to determine the cause of the failure.

NotSupportedException

There is already a read operation in progress.

ObjectDisposedException

This object has been closed.

InvalidOperationException

Authentication has not occurred.

The method reads a maximum of count bytes from the stream and stores them in buffer beginning at offset. You cannot perform multiple simultaneous read operations.

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

To perform this operation asynchronously, use the BeginRead method.

The following code example demonstrates reading from an SslStream.

static string ReadMessage(SslStream sslStream)
{
    // Read the  message sent by the server. 
    // The end of the message is signaled using the 
    // "<EOF>" marker.
    byte [] buffer = new byte[2048];
    StringBuilder messageData = new StringBuilder();
    int bytes = -1;
    do
    {
        bytes = sslStream.Read(buffer, 0, buffer.Length);

        // Use Decoder class to convert from bytes to UTF8 
        // in case a character spans two buffers.
        Decoder decoder = Encoding.UTF8.GetDecoder();
        char[] chars = new char[decoder.GetCharCount(buffer,0,bytes)];
        decoder.GetChars(buffer, 0, bytes, chars,0);
        messageData.Append (chars);
        // Check for EOF. 
        if (messageData.ToString().IndexOf("<EOF>") != -1)
        {
            break;
        }
    } while (bytes != 0); 

    return messageData.ToString();
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft