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: enter image description here

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!

1

There are 1 best solutions below

0
On

Either:

  1. You have sent data to a connection that had already been closed by the peer
  2. The peer closed the connection while there was still unread data in its socket receive buffer
  3. The peer is running on Windows and its process has exited.
  4. The peer has deliberately reset the connection by means which I will not detail here.
  5. One of various host or network conditions has arisen such as connection timeout or failure.