C# SslStream 与 GZipStream

发布于 2024-09-19 20:44:07 字数 323 浏览 7 评论 0原文

是否可以在 C# 中使用 GZipStream 传递 SslStream?也就是说


GZipStream stream = new GZipStream(sslStream, CompressionMode.Compress);
stream.Write(...);

...

GZipStream stream = new GZipStream(sslStream, CompressionMode.Decompress);
stream.Read(...);

,如果可以的话,SslStream 在此之后仍处于可用状态还是应该关闭?

谢谢。

Is it possible to use GZipStream passing an SslStream in C#? i.e. can you do


GZipStream stream = new GZipStream(sslStream, CompressionMode.Compress);
stream.Write(...);

...

GZipStream stream = new GZipStream(sslStream, CompressionMode.Decompress);
stream.Read(...);

If this is possible, is the SslStream still in a useable state after this or should it be closed?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

冷︶言冷语的世界 2024-09-26 20:44:23

这是可能的,有时可能有效,但有时也可能失败。如果您的协议始终允许您在 SSLStream 关​​闭之前“彻底关闭”GZipStream,那么它应该可以工作。我所说的“完全关闭”是指 GZipStream 关​​闭效果(压缩缓冲区的刷新)可以发生并传播到对等点。它可能会失败,因为 Ssl 允许相当激烈的闭包语义。请参阅 RFC 2246 的第 7.2.1 节。

It is possible and may sometimes work, but it also may sometimes fail. It should work if your protocol always allows you to "cleanly close" the GZipStream before the SSLStream closes. By "cleanly close" I mean that the GZipStream closure effects (the flushing of the compression buffer) can occur and propogate to the peer. It may fail because Ssl allows for rather drastic closure semantics. See section 7.2.1 of RFC 2246.

巴黎夜雨 2024-09-26 20:44:20

我想不出为什么不; SSL 流只会被给予字节,但这正是它所期望的。重要的是正确关闭 GZipStream,因为即使 Flush() 也不总是真正刷新(由于必须保留运行缓冲区以进行压缩)。所以:

using(GZipStream stream = new GZipStream(sslStream, CompressionMode.Compress)) {
    stream.Write(...);
    stream.Close(); // arguably overkill; the Dispose() from "using" should be enough
}

我还假设您没有读取和写入同一个 SSL 流(即,这是两个单独的示例,而不是一个示例),因为这并不听起来不太可能起作用。

I can't think why not; the SSL stream will just be given bytes, but that is what it expected anyway. The important thing is to close the GZipStream properly, as even Flush() doesn't always really flush (due to having to keep a running buffer for compression). So:

using(GZipStream stream = new GZipStream(sslStream, CompressionMode.Compress)) {
    stream.Write(...);
    stream.Close(); // arguably overkill; the Dispose() from "using" should be enough
}

I'm also assuming that you aren't reading and writing to the same SSL stream (i.e. that is two separate examples, not one example), as that doesn't sound likely to work.

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