android 原生Socket接收数据时出现大包

xiaobeiwen 发布于 2017/05/23 14:56
阅读 118
收藏 0
现在通讯 格式是报文长度 + 报文内容 ;客户端接收数据时先读取报文长度再去读取报文内容,目前的情况是时而读到超级长的包长导致OOM了,

 下面是代码:代码有点乱,还没做整理,这个是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();
                            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();
                                        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();
                                    baseProto obj = ClassFactory.getProtoObject(alias);
                                    if (obj != null) {
                                        obj.parseFrom(br);
                                        EventBus.getDefault().post(new ResEvent(obj));
                                    }
                                }
                            }
                        }
                    }
                }
                if (mDos != null) mDos.close();
                if (mDis != null) mDis.close();
                if (mSocket != null)
                    mSocket.close();
                this.isRunning = false;
                reconnect();
            } catch (EOFException e) {
                stopWork();
                reconnect();
                e.printStackTrace();
            } catch (SocketException e) {
                stopWork();
                reconnect();
                e.printStackTrace();
            } catch (UnknownHostException e) {
                stopWork();
                reconnect();
                e.printStackTrace();
            } catch (IOException e) {
                stopWork();
                reconnect();
                e.printStackTrace();

            }
        }

出现大包的原因可能是粘包引起的,这个问题要怎么处理!

 

加载中
返回顶部
顶部