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
public int Read (
	byte[] buffer, 
	int offset, 
	int count
public override function Read (
	buffer : byte[], 
	offset : int, 
	count : int
) : int
Not applicable.



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


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


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

Return Value

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

Exception typeCondition


buffer is a null reference (Nothing in Visual Basic).


offset < 0.


offset > the length of buffer.


offset + count > the length of buffer.


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


There is already a read operation in progress.


This object has been closed.


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;
        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)
    } while (bytes != 0); 
    return messageData.ToString();

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

© 2015 Microsoft