Socket 接收字节报错: System.OutOfMemoryException:“Exception_WasThrown” - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
atfeel
V2EX    C#

Socket 接收字节报错: System.OutOfMemoryException:“Exception_WasThrown”

  •  
  •   atfeel 2022-10-02 01:12:08 +08:00 1890 次点击
    这是一个创建于 1156 天前的主题,其中的信息可能已经有所发展或是发生改变。
    以下是读取 Socket 的粘连接收代码

    private List<byte> PacketData = new List<byte>();//包池
    private void OnReceive(Socket client, byte[] bytes)
    {
    PacketData.AddRange(bytes);//添加数据
    while (true)
    {
    if (PacketData.Count < 4) return; //数据不够读取包长,响应字节留给下一次在读取
    byte[] cOntentLenBytes= PacketData.GetRange(0, 4).ToArray();//前 4 字节是包长
    int len = BitConverter.ToInt32(contentLenBytes, 0);//转换 int

    if (PacketData.Count < len) return;//包长不完整,就忽略本次读取,留给下一次读取




    //这句报错:System.OutOfMemoryException:“Exception_WasThrown”
    var pack = PacketData.GetRange(4, len);//读取包内容




    byte[] fullData = pack.ToArray();

    PacketData.RemoveRange(0, len+4);//移除已读取的内容
    }
    }



    上面 var pack = PacketData.GetRange(4, len);这句报 System.OutOfMemoryException:“Exception_WasThrown”的错,一开始运行正常,当运行 2 个小时左右就会报这个错。不知道是什么原因。。。
    试了各种方法都无法解决。。头疼
    4 条回复    2022-10-02 23:17:27 +08:00
    BrettD
        1
    BrettD  
       2022-10-02 02:04:31 +08:00 via iPhone
    有没有可能是真的 OutOfMemoryException……
    atfeel
        2
    atfeel  
    OP
       2022-10-02 10:42:23 +08:00
    @BrettD 这个代码已经是相当简单了,我实在检查不出内存出现异常。不知道大家是怎么写粘连包的
    qianlv7
        3
    qianlv7  
       2022-10-02 23:16:00 +08:00 via iPhone
    越界了啊,假设 count == len 情况,GetRange 的范围[4 ,len+4 )啊,
    qianlv7
        4
    qianlv7  
       2022-10-02 23:17:27 +08:00 via iPhone
    实际收到只有 len ,而 len+4 就越界了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1073 人在线   最高记录 669       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 33ms UTC 17:37 PVG 01:37 LAX 09:37 JFK 12:37
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86