SslStream.Read(Byte[], Int32, Int32) 메서드

정의

이 스트림에서 데이터를 읽어 지정된 배열에 저장합니다.

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

매개 변수

buffer
Byte[]

이 스트림에서 읽은 바이트를 받는 Byte 배열입니다.

offset
Int32

이 스트림에서 읽은 데이터를 저장하기 시작하는 buffer 내의 위치(0부터 시작)가 포함된 Int32입니다.

count
Int32

이 스트림에서 읽을 최대 바이트 수가 포함된 Int32입니다.

반환

읽은 바이트 수를 지정하는 Int32 값입니다. 읽을 데이터가 더 이상 없으면 0이 반환됩니다.

예외

buffer이(가) null인 경우

offset가 0보다 작은 경우

또는

offsetbuffer의 길이보다 큽니다.

또는

offset과 count의 합이 buffer 길이보다 큽니다.

읽기 작업이 실패한 경우. 내부 예외가 있는 경우, 이를 검토하여 오류의 원인을 확인합니다.

읽기 작업을 진행 중인 경우

이 개체가 닫힌 경우.

인증이 수행되지 않은 경우.

예제

다음 코드 예제에서는 에서 읽는 방법을 보여 줍니다 SslStream.

private:
    static String^ ReadMessage(SslStream^ sslStream)
    {
          
        // Read the  message sent by the server.
        // The end of the message is signaled using the
        // "<EOF>" marker.
        array<Byte>^ buffer = gcnew array<Byte>(2048);
        StringBuilder^ messageData = gcnew StringBuilder;
        // Use Decoder class to convert from bytes to UTF8
        // in case a character spans two buffers.
        Encoding^ u8 = Encoding::UTF8;
        Decoder^ decoder = u8->GetDecoder();

        int bytes = -1;
        do
        {
            bytes = sslStream->Read(buffer, 0, buffer->Length);
             
            array<__wchar_t>^ chars = gcnew array<__wchar_t>(
                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();
    }
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();
}
Private Shared Function ReadMessage(sslStream As SslStream) As String

    ' Read the  message sent by the server.
    ' The end of the message is signaled using the "<EOF>" marker.
    Dim buffer = New Byte(2048) {}
    Dim messageData = New StringBuilder()
    Dim bytes As Integer

    Do
        bytes = sslStream.Read(buffer, 0, buffer.Length)

        ' Use Decoder class to convert from bytes to UTF8
        ' in case a character spans two buffers.        
        Dim decoder As Decoder = Encoding.UTF8.GetDecoder()
        Dim chars = New Char(decoder.GetCharCount(buffer, 0, bytes) - 1) {}
        decoder.GetChars(buffer, 0, bytes, chars, 0)
        messageData.Append(chars)

        ' Check for EOF.
        If messageData.ToString().IndexOf("<EOF>") <> -1 Then Exit Do
        
    Loop While bytes <> 0

    Return messageData.ToString()

End Function

설명

메서드는 스트림에서 최대 count 바이트를 읽고 부터 offset에 저장합니다buffer. 여러 동시 읽기 작업을 수행할 수 없습니다.

성공적으로 인증될 때까지 이 메서드를 호출할 수 없습니다. 인증하려면 , 또는 BeginAuthenticateAsClient, AuthenticateAsServerBeginAuthenticateAsServer 메서드 중 AuthenticateAsClient하나를 호출합니다.

이 작업을 비동기적으로 수행하려면 메서드를 BeginRead 사용합니다.

적용 대상