Our project implements ourself long-connection framework base on NIO for push, it used to work properly. Lately, there are some problem, "SocketChannel.read(byteBuffer)" throw exception "java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)", details are as follows:
the image contains error message
The code for error as follows:
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;
}
}
}
I've looked for a lot of similar error, but not find a good idea to solve the problem, how can I do? Thanks for any of your anwsers!
Either: