开发者

Read contents from SslStream

开发者 https://www.devze.com 2023-04-12 08:42 出处:网络
I have an application using which I execute IMAP commands using: TcpClient to connect to the IMAP server

I have an application using which I execute IMAP commands using:

  • TcpClient to connect to the IMAP server
  • SslStream to write and read commands

Problem:

  • Cannot read the complete ouput content from the stream
  • while loop on the SslStream.Read s开发者_运维百科eems not to work
  • StreamReader.ReadLine, ReadToEnd, Read methods do not work

Sample code:

while ((l = reader.ReadLine()) != null)
{
    output.AppendLine(l);
}

This code snippet would read 1 to 2 lines and hang in reader.Readline().

Workaround I tried with setting the ReadTimeout property:

try
{
    _output=new byte[_tcpclient.ReceiveBufferSize];
    _sslstream.Read(_output, 0, _output.Length);
    textBox1.Text = Encoding.ASCII.GetString(_output);
}
catch (IOException ex)
{
    textBox1.Text ="ERROR !! " + ex.Message;                
}

Help:

  • How can I read the complete output of a command from the stream?

Note: I do not want to use any third party libraries.


The TCP stream cannot know whether the current response has finished. All it knows is whether it has just received data on the wire; it cannot know whether the next packet is going to come right now (a multi-packet response) or whether it will come much later (if the response is finished).

Instead, you need to predict when you'll get more data; you should keep reading until you receive a tagged completion response, as documented in the IMAP protocol.

However, IMAP seems to be intended to be read continuously on a background thread, since the server can send you information at any time. Therefore, you probably ought to have a separate thread which is always in ReadLine().

0

精彩评论

暂无评论...
验证码 换一张
取 消