1

Guys i'm using SslStream as a server to test my app, but i have issues reading from the stream. I'm using the following code:

        while (true)
        {
            int read = sslStream.Read(buffer, 0, buffer.Length);

            string bufferString = System.Text.Encoding.Default.GetString(buffer);

            // Check for End?
            if (bufferString.IndexOf("\n\r\n", System.StringComparison.Ordinal) != -1)
            {
                break;
            }
        }

The problem is that the first loop returns:

G\0\0\0\0\0

and the second run returns:

ET /whateverman

while the result should be

GET /whateverman

What is the issue and is there a better way to read from an SslStream?

Anonymous
  • 748
  • 3
  • 10
  • 22

1 Answers1

2

Result is exactly as expected (and not directly related to SSL stream) - you are converting bytes that you did not read.

If you want to manually read strings from Stream you must respect result of Read call that tells you home many bytes actually read from the stream.

string partialString = System.Text.Encoding.Default.GetString(buffer, 0, read);

And than don't forget to concatenate strings.

Note: using some sort of reader (StreamReader/BinaryReader) may be better approach.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179