Android java套接字读取返回eof,即使在重新连接时也是如此

Android java socket read return eof, even on reconnect

提问人:kyesia 提问时间:6/13/2023 更新时间:6/13/2023 访问量:29

问:

Java TCP 客户端套接字输入 read() 返回 -1。如果返回 -1,请重新连接。 重新连接成功,但再次返回 -1。和 每 10 秒向服务器发送一次 keepAlive 我终止程序并再次连接,连接成功并接收到数据。 我无法弄清楚原因。

如果您能就此事给我答复,我将不胜感激。

private class SocketThread extends Thread{
        private Socket mSocket = null;
        private InputStream mInStream = null;
        private OutputStream mOutStream = null;

        public SocketThread(){

        }

        public void run(){
            byte[] buffer = new byte[1024];
            int bytes = 0;

            try {
                InetAddress serverAddress = InetAddress.getByName(mHostAddr);

                mSocket = new Socket(serverAddress, mHostPort);
                mInStream = mSocket.getInputStream();
                mOutStream = mSocket.getOutputStream();
                mSocket.setSoTimeout(3000);
            }
            catch(Exception e){
                Log.e(MainActivity.TAG, "connection error", e);
                e.printStackTrace();

                mState = STATE_NONE;
                mHandler.obtainMessage(SOCKET_STATE_CHANGE, mState, -1).sendToTarget();
                return;
            }

            mState = STATE_CONNECTED;
            mHandler.obtainMessage(SOCKET_STATE_CHANGE, mState, -1).sendToTarget();

            while(true){
                try{
                    bytes = mInStream.read(buffer);

                    if(bytes >= 0) {
                        mHandler.obtainMessage(SOCKET_READ, bytes, -1, buffer).sendToTarget();
                    } else if (bytes == -1) {
                        this.cancel();
                        throw new SocketTimeoutException();
                    }

                    sleep(10);
                }
                catch (SocketTimeoutException e){
                    Log.e(MainActivity.TAG, "disconnected 3", e);
                    e.printStackTrace();

                    mHandler.obtainMessage(SOCKET_RECONNECT, mState, -1).sendToTarget();
                    break;
                }
                catch(IOException e){
                    Log.e(MainActivity.TAG, "disconnected", e);
                    e.printStackTrace();
                    mState = STATE_NONE;
                    mHandler.obtainMessage(SOCKET_STATE_CHANGE, mState, -1).sendToTarget();
                    break;
                }
                catch(InterruptedException e){
                    Log.e(MainActivity.TAG, "disconnected 2", e);
                    e.printStackTrace();
                    mState = STATE_NONE;
                    mHandler.obtainMessage(SOCKET_STATE_CHANGE, mState, -1).sendToTarget();
                    break;
                }
            }
        }
    public void cancel(){
        if(mState != STATE_CONNECTED)
            return;
        try{
            mInStream.close();
            mOutStream.close();
            mSocket.close();
        }
        catch (IOException e){
            Log.e(MainActivity.TAG, "close failed", e);
            e.printStackTrace();
        }
    }
    case TCPClient.SOCKET_RECONNECT:
                    connect(true);
                    break;
public void connect(boolean connect)
    {
        if (connect)
        {
            if (mRelaySock.getStatus() == TCPClient.STATE_CONNECTED)
            {
                mRelaySock.stop();
            }
            mRelaySock.setHostInfo(mHostAddr, mHostPort);
            mRelaySock.start();
        }
        else
        {
            mRelaySock.stop();
        }
    }
Android 套接字 EOF 重新连接

评论

1赞 user207421 6/13/2023
InputStream.read()返回 -1 表示流结束。对等方已关闭连接。这与读取超时无关,您这样对待它是错误的。除非缓冲区长度为零或提供长度为零,否则它永远不会返回零。如果您不断收到流结束,则服务器会不断关闭您的连接。对此进行调查。

答: 暂无答案