我们的项目实现了基于NIO for push的自己的长连接框架,它曾经正常工作。最近,有一些问题,“SocketChannel.read(byteBuffer)”抛出异常“java.net.SocketException:recvfrom failed:ECONNRESET(Connection peer by peer)”,详细信息如下:
图片包含错误消息
错误代码如下:
private void read(ByteBuffer byteBuffer, int length) throws Exception {
int count = 0;
long readBeginMills = SystemClock.elapsedRealtime();
while (count < length) {
try {
int readCount = mSocketChannel.read(byteBuffer);
long nowMills = SystemClock.elapsedRealtime();
if (readCount > 0) {
count += readCount;
readBeginMills = nowMills;
}
//1.readCount为-1时是连接断开了,直接报错重连
//2.如果读取数据超过了20s也报错重连
if(nowMills - readBeginMills >= 20000 || readCount == -1){
throw new LostTcpByteException("byte lost exception,need to shutdown and reconnection");
}
} catch (Exception e) {
throw e;
}
}
}
我找了很多类似的错误,但没有找到解决问题的好主意,我该怎么办?
谢谢你的任何一个人!
答案 0 :(得分:3)
或者: