sslStream.Read问题:读取的所有字节均为0

发布于 2024-11-16 05:30:41 字数 762 浏览 2 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

依 靠 2024-11-23 05:30:41

buffer 中尚未被 Read 调用填充的所有字节都将为零;这是标准的 C#。

如果其中的最后一个字节为零,则只有两件事可能导致:

  • 您从流中读取真正的空字节(不太可能)
  • Read 不读取任何内容(在这种情况下它返回 0——你绝对应该检查返回值)

All bytes in buffer that have not been filled in by the Read call will be zero; this is standard C#.

If every last one byte in there is zero, only two things can be responsible:

  • You read real null bytes from the stream (unlikely)
  • Read does not read anything (in which case it returns 0 -- you should definitely be checking the return value)
总以为 2024-11-23 05:30:41

bytes2 = sslStream.Read(buffer, 0, 4096);最多 4096 字节读取到buffer 中,而不是正好 4096 字节。它会阻塞,直到至少读取一个字节并返回读取的字节数。因此,在方法调用之后,buffer 将具有与方法调用之前相同的内容(例如,填充空值),除了第一个 bytes2 字节,即字节从服务器收到。

bytes2 = sslStream.Read(buffer, 0, 4096); reads up to 4096 bytes into buffer, 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 first bytes2 bytes, which are the bytes received from the server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文