双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

socket通讯时出现大包

xiaobeiweng xiaobeiweng 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
正常情况下包的长度是9~500左右的长度的,后台基本不会发来大包,后台发送日志也是正常的,但是android 端就经常出现这个情况,要怎么处理,看了网上的文章,说是粘包,也提到一些思路,我这边是用自定义协议的,结合我的项目用代码实现思路也不清晰,希望大神路过指点一二。

下面是错误信息
java.lang.OutOfMemoryError: Failed to allocate a 842019138 byte allocation with 16777216 free bytes and 481MB until OOM
at fuhao.com.app.net.TcpSocketZ$ReceiveThread.run(TcpSocketZ.java:255)


下面是代码:代码有点乱,还没做整理,这个是TcpSocket实现Runnable的run方法:
 @Override
        public synchronized void run() {
            try {
                int packetLen = 0;
                while (isRunning) {
                    if (!isConnected) {
                        mSocket = new Socket(HostIP, Port);
                        mSocket.setSendBufferSize(1024);
                        mSocket.setReceiveBufferSize(1024);
                        mSocket.setTcpNoDelay(true);
                        mSocket.setKeepAlive(true);
                        mDos = new DataOutputStream(mSocket.getOutputStream());
                        mDis = new DataInputStream(mSocket.getInputStream());
                        handshark();
                        isConnected = true;
                        startHeartBeat();
                    } else {
                        if (mDis.available() >= 4) {
                            packetLen = mDis.readInt();
                            L.e("TcpSocketZ", "-----packetLen------" + packetLen);
                            if (packetLen > 0) {
                                byte iszlib = mDis.readByte();
                                if (iszlib == (byte) 1) {
                                    try {
                                        byte[] inputData = new byte[packetLen - 1];
                                        utils.readData(mDis, inputData, 0, packetLen - 1);
                                        byte[] result = CompressUtil.decompress(inputData);
                                        byteReader br = new byteReader(result);
                                        int alias = br.readInt();
                                        L.e("TcpSocketZ", "recv packet zlib ------" + String.valueOf(alias));
                                        baseProto obj = ClassFactory.getProtoObject(alias);
                                        if (obj != null) {
                                            obj.parseFrom(br);
                                            EventBus.getDefault().post(new ResEvent(obj));
                                        }
                                    } catch (DataFormatException e) {
                                        e.printStackTrace();
                                    }
                                } else {
                                    byte[] inputData = new byte[packetLen - 1];
                                    utils.readData(mDis, inputData, 0, packetLen - 1);
                                    byteReader br = new byteReader(inputData);
                                    int alias = br.readInt();
                                    L.e("TcpSocketZ", "packet alias------" + String.valueOf(alias));
                                    if (alias == 20002) {
                                        backTime = 0;
                                    }
                                    baseProto obj = ClassFactory.getProtoObject(alias);
                                    if (obj != null) {
                                        obj.parseFrom(br);
                                        EventBus.getDefault().post(new ResEvent(obj));
                                    }
                                }
                            }
                        }
                    }
                }
                mDos.close();
                mDis.close();
                if (mSocket != null) {
                    mSocket.close();
                    mSocket = null;
                }
                this.isRunning = false;
                reconnect();
            } catch (EOFException e) {
                this.isConnected = false;
                try {
                    mDos.close();
                    mDis.close();
                    if (mSocket != null) {
                        mSocket.close();
                        mSocket = null;
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                reconnect();
                e.printStackTrace();
            } catch (SocketException e) {
                e.printStackTrace();
                this.isConnected = false;
                try {
                    mDos.close();
                    mDis.close();
                    if (mSocket != null) {
                        mSocket.close();
                        mSocket = null;
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                reconnect();
                e.printStackTrace();
            } catch (UnknownHostException e) {
                e.printStackTrace();
                this.isConnected = false;
                try {
                    mDos.close();
                    mDis.close();
                    if (mSocket != null) {
                        mSocket.close();
                        mSocket = null;
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                reconnect();
                e.printStackTrace();
            } catch (IOException e) {
                this.isConnected = false;
                try {
                    mDos.close();
                    mDis.close();
                    if (mSocket != null) {
                        mSocket.close();
                        mSocket = null;
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                reconnect();
                e.printStackTrace();
 
            }
        }


错误的是出现在上面代码的第40行
...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部
xiaobeiweng
Java SE 62,621 成员 307,251 内容