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");
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?
For those that come across this and it didn't answer your question, I found the following to solve the issue of the Stream.Read() blocking the thread.
NetworkStream stream = client.GetStream();
while (true)
{
while (stream.DataAvailable == true)
{
input = stream.Read();
// --- do whatever with the input
}
}
For the life of me I couldn't figure out why the thread was stopping. Especially when the tooltip popup in VS says,
Reads a byte from the stream and advances the position in the stream
by one byte, or returns -1 if at the end of the stream.
It says nothing about it blocking or not giving the -1 unless the stream is closed.
精彩评论