sslStream.Read问题:读取的所有字节均为0
TcpClient client = new TcpClient("69.147.112.160", 443);
SslStream sslStream = new SslStream(client.GetStream(),false,
ValidateServerCertificate,null);
try
{
sslStream.AuthenticateAsClient("mail.yahoo.com");
}
catch (AuthenticationException e)
{
return;
}
byte[] messsage = Encoding.UTF8.GetBytes(".<EOF>");
sslStream.Write(messsage);
sslStream.Flush();
byte[] buffer = new byte[4096];
int bytes2 = -1;
do
{
/**************************************************
*** JUST A LINE BELOW ALL buffer BYTES ARE ZERO!**
*************************************************/
bytes2 = sslStream.Read(buffer, 0, 4096);
m_sockClient.Send(buffer, bytes2, 0);
} while (bytes != 0);
TcpClient client = new TcpClient("69.147.112.160", 443);
SslStream sslStream = new SslStream(client.GetStream(),false,
ValidateServerCertificate,null);
try
{
sslStream.AuthenticateAsClient("mail.yahoo.com");
}
catch (AuthenticationException e)
{
return;
}
byte[] messsage = Encoding.UTF8.GetBytes(".<EOF>");
sslStream.Write(messsage);
sslStream.Flush();
byte[] buffer = new byte[4096];
int bytes2 = -1;
do
{
/**************************************************
*** JUST A LINE BELOW ALL buffer BYTES ARE ZERO!**
*************************************************/
bytes2 = sslStream.Read(buffer, 0, 4096);
m_sockClient.Send(buffer, bytes2, 0);
} while (bytes != 0);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
buffer
中尚未被Read
调用填充的所有字节都将为零;这是标准的 C#。如果其中的最后一个字节为零,则只有两件事可能导致:
Read
不读取任何内容(在这种情况下它返回0
——你绝对应该检查返回值)All bytes in
buffer
that have not been filled in by theRead
call will be zero; this is standard C#.If every last one byte in there is zero, only two things can be responsible:
Read
does not read anything (in which case it returns0
-- you should definitely be checking the return value)bytes2 = sslStream.Read(buffer, 0, 4096);
将最多 4096 字节读取到buffer
中,而不是正好 4096 字节。它会阻塞,直到至少读取一个字节并返回读取的字节数。因此,在方法调用之后,buffer
将具有与方法调用之前相同的内容(例如,填充空值),除了第一个bytes2
字节,即字节从服务器收到。bytes2 = sslStream.Read(buffer, 0, 4096);
reads up to 4096 bytes intobuffer
, not exactly 4096 bytes. It blocks until at least one byte is read and returns the number of bytes read. So after the method call,buffer
will have the same content as before the method call (e.g., filled with nulls), except for the firstbytes2
bytes, which are the bytes received from the server.