Android socket通讯时出现大包

xiaobeiweng 2017-05-19 05:38:18
正常情况下包的长度是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();

}
}
...全文
169 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaobeiweng 2017-05-20
  • 打赏
  • 举报
回复
xiaobeiweng 2017-05-19
  • 打赏
  • 举报
回复
提示
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)
错误的是在上面代码的40行

80,360

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧