如何通过在句柄中重复的 AsyncTask 中的套接字读取来避免 EOFExeption?

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

我需要每秒从服务器更新数据。我用 AsyncTask 创建句柄,每秒重复一次。这行得通,但是大约一分钟它通过从 DataInputStream 读取 EOFException 来粉碎。 处理程序

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 );

异步任务

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

}

有人知道为什么出现 EOFException 大约一分钟吗?如何避免? P.S。要从服务器获取必要的信息,我必须先通过认证

为什么会出现这个异常?

根据文档

Signals that an end of file or end of stream has been reached unexpectedly during input. This exception is mainly used by data input streams to signal end of stream. Note that many other input operations return a special value on end of stream rather than throwing an exception.

看看这个answer

问题已通过更改方法解决。我在 AsyncTask

中添加了两个用于输出和输入的线程