我们的项目实现了自己的基于NIO的长连接框架进行推送,它曾经可以正常工作。最近出现了一些问题,"SocketChannel.read(byteBuffer)" throw exception "java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset 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;
}
}
}
我找了很多类似的错误,但没有找到解决问题的好主意,我该怎么办?感谢您的任何回答!