Search code examples
c#network-programmingimap

SslStream.ReadByte() blocks thread?


I'm trying to write an Imap4 client.
For that I use a SslStream to Connect to the Server.
Everything's fine until I send the "Login" command.
When I try to get an Answer to it, SslStream.ReadByte() block the thread.
The result is that my programm crashes always.
Whats happening here??

Code:

 if (ssl)
{
     s = stream;
}

int cc = 0; MessageBox.Show("entered"); while (true) { int xs = s.ReadByte(); MessageBox.Show(xs.ToString()); if (xs > 0) { buf.Add((byte)xs); cc++; if (xs == '\n') { break; } if (cc > 10) MessageBox.Show(en.GetString(buf.ToArray())); } else { break; } } MessageBox.Show("left");

Solution

  • Yes, Stream.ReadByte() will block until either:

    • The stream is closed (in which case -1 is returned)
    • Data is received on the stream, in which case the next byte will be returned

    So presumably the server you're connecting to isn't sending any data... it may well be waiting for more data from you. How convinced are you that your login command is being sent properly? Have you flushed the stream you're writing on? Sent any delimiters or line terminators that are required?