【发布时间】:2016-08-07 22:46:22
【问题描述】:
读取TCP SSL example from MSDN后,他们使用字节数组将数据读入流中。数组限制为 2048 是否有原因?如果 TCP 发送比 2048 更长的数组怎么办?此外,buffer.Length 属性如何继续读取流,因为它正在发生变化。这对我来说完全没有意义。为什么要读取缓冲区的长度,难道你不想读取进入流的增量字节的长度吗?
static string ReadMessage(SslStream sslStream)
{
// Read the message sent by the client.
// The client signals the end of the message using the
// "<EOF>" marker.
byte [] buffer = new byte[2048];
StringBuilder messageData = new StringBuilder();
int bytes = -1;
do
{
// Read the client's test message.
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 or an empty message.
if (messageData.ToString().IndexOf("<EOF>") != -1)
{
break;
}
} while (bytes !=0);
return messageData.ToString();
}
【问题讨论】: