Android java socket read return eof, even on reconnect

36 views Asked by At

java tcp client socket input read() returns -1. If return -1, reconnect again. Reconnection succeeded, but -1 is returned again. and Send keepAlive to server every 10 seconds I terminate the program and connect again, the connection is succeed and data is received. I can't figure out the cause.

I would appreciate it if you could send me a reply on this matter.

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();
        }
    }
0

There are 0 answers