Search code examples
androidsocketsandroid-asynctaskeofexception

How to avoid EOFExeption by read from socket in AsyncTask that repeats in handle?


I need to update data from server every second. I create handle with AsyncTask, that repeats every second. This works, but about a minute it crushes with EOFException by reading from DataInputStream. Handler

handler.postDelayed( new Runnable() {

              @Override
              public void run() {

                  tickListAdapter = new TickListAdapter(TickActivity.this,tickList);
                  tickListView.setAdapter(tickListAdapter);
                  AsyncTCPSend tcpSend= new AsyncTCPSend(address,serverPort, line);
                  tcpSend.execute();
                  Log.e(TAG,"update");
                  handler.postDelayed( this, 1000 );
              }
          },1000 );

AsyncTask

    public class AsyncTCPSend extends AsyncTask<Void, Void, Void> {
        String address;
        int port;
        String message;
        String response=null;String lineRead = null;
        Socket socket;
        int count=1;
        OutputStream os;
        DataInputStream  dis;

        AsyncTCPSend(String addr, int p, String mes) {
            address = addr;
            port = p;
            message = mes;
        }

        @Override
        protected Void doInBackground(Void... params) {
            socket = null;
            try {
                socket = new Socket(address, port);
                os = socket.getOutputStream();
                dis = new DataInputStream(socket.getInputStream());
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } 
            if (!isConnect){
                if (connect()){
                    isConnect = true;
                    Log.e("CLIENT","now is connected");
                    getTick();
                }
            } 
             return null;
        }

        public boolean connect(){
            try {

                Log.e("CLIENT","CONNECTED");
                String command = "AUTH_START";

                byte[] queryBody = null;
                queryBody = command.getBytes("UTF-16LE");

                byte[] message = new byte[queryBody.length];
                for (int i = 0; i < message.length; ++i)
                os.write(message,0,message.length);
                Log.e(TAG,"AUTH_START");
                String srv = null;
                srv = function();
                if (srv != null){
                    if (srv.equals("Error")){
                         Log.e("CLIENT","Error");
                        return false;
                    } else {

                        String auth_answer = "AUTH_ANSWER";
                        byte[]  authBody = auth_answer.getBytes("UTF-16LE");
                        Log.e(TAG,"AUTH_ANSWER");
                        os.write(authBody,0,authBody.length);
                        srv = function();
                        if (srv.equals("Error")){
                         Log.e("CLIENT","Error");
                         return false;
                        } else {
                            Log.e(TAG,"AUTH  SUCCESS!!!");
                         return true;

                        }
                    }
                } else {
                    return false;
                }
            }
                 catch (UnknownHostException e) {
                    e.printStackTrace();
                     return false;
                } catch (IOException e) {
                    e.printStackTrace();
                     return false;
                }
        }

        public String getTick(){
            String tick = "TICK";
            try {

                tickBody = tick.getBytes("UTF-16LE");
                os.write(tickBody,0,tickBody.length);
                String srv = function();
                return srv;

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return null;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }

        }

        String function(){
            String srv = null;
            int len = 0;
            try {

                len = dis.readInt(); //EOFException is here!!!!
                    byte[] data = new byte[1024];
                if (len > 0) {
                    dis.read(data, 0, data.length);
                }
                String out = new String(data,"UTF-16");
                    if (out.indexOf("Done")>0){
                        if (out.indexOf("STAT")>0 ||out.indexOf("AST")>0){                      
                            srv = out;
                        } 
                        else {                                                                                      
                            srv = out.substring(out.indexOf("SRV")+9,out.indexOf("SRV")+41);
                        }
                    } else {
                        srv = "Error";
                    }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return srv;

        }


        @Override
        protected void onPostExecute(Void result) {
            Log.e("CLIENT","onPostExecute");
            super.onPostExecute(result);
        }
    }

}

Anybody know why appear EOFException about a minute? How to avoid? P.S. To obtain the necessary information from the server, I must first pass authentication


Solution

  • The problem was solved by changing the method. I added two threads for output and input inside AsyncTask