You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 8 C#中SslStream报“无法确定帧大小或收到损坏帧”问题求助

.NET 8 SslStream安全TCP通信认证错误排查求助

我有两个部署在不同服务器上的.NET 8 C#应用程序,尝试通过SslStream实现安全TCP通信,但在应用间认证环节遇到错误,希望得到解决指导。

我的消息读取代码如下:

string ReadMessage(SslStream sslStream)
{
    // 读取服务器发送的消息
    // 消息结尾以"<EOF>"标记标识
    byte[] buffer = new byte[2048];
    StringBuilder messageData = new StringBuilder();
    int bytes = -1;
    do
    {
        bytes = sslStream.Read(buffer, 0, buffer.Length);

        // 使用Decoder类将字节转换为UTF8
        // 处理跨缓冲区的字符
        Decoder decoder = Encoding.UTF8.GetDecoder();
        char[] chars = new char[decoder.GetCharCount(buffer, 0, bytes)];
        decoder.GetChars(buffer, 0, bytes, chars, 0);
        messageData.Append(chars);
        // 检查是否到达消息结尾
        if (messageData.ToString().Contains("<EOF>", StringComparison.CurrentCulture))
        {
            break;
        }
    } while (bytes != 0);

    return messageData.ToString();
}

执行bytes = sslStream.Read(buffer, 0, buffer.Length)时出现以下错误:

System.Security.Authentication.AuthenticationException: Cannot determine the frame size or a corrupted frame was received.

at System.Net.Security.SslStream.GetFrameSize(ReadOnlySpan1 buffer)

at System.Net.Security.SslStream.EnsureFullTlsFrameAsync[TIOAdapter](CancellationToken cancellationToken, Int32 estimatedSize)

at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder1.StateMachineBox1.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)

at System.Net.Security.SslStream.ReadAsyncInternal[TIOAdapter](Memory1 buffer, CancellationToken cancellationToken)
--- End of inner exception stack trace ---

at System.Net.Security.SslStream.ReadAsyncInternal[TIOAdapter](Memory1 buffer, CancellationToken cancellationToken)

at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder1.StateMachineBox1.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)

at System.Net.Security.SslStream.Read(Byte[] buffer, Int32 offset, Int32 count)

我已确认以下连接和证书信息:

  • 协议:Tls12
  • 已作为服务器认证:True
  • 已加密:True
  • 可读取:True
  • 可写入:True

内容的提问来源于stack exchange,提问作者codeMonkey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 02:24:56