简体   繁体   English

为什么InputStream停止我的应用程序?

[英]Why InputStream is stopping my application?

I have an Android App that talks with a Java Server between socket connection. 我有一个Android应用程序,它在套接字连接之间与Java Server进行通信。 When server is running, there's no problem. 服务器运行时,没有问题。 I send something from Android and server answer. 我从Android和服务器答案中发送了一些信息。 I get response and put it in Toast. 我得到回应并将其放入Toast。 Looks great! 看起来很棒! But if i stop running server, Android stay waiting for answer and all application stop working. 但是,如果我停止运行服务器,Android将继续等待答案,并且所有应用程序都将停止工作。 Strange is the fact that application in running inside a thread, so i think its not supposed to happen. 奇怪的是,应用程序在线程中运行,因此我认为它不应该发生。 I'd like to know if is there some way to avoid it to crash application and some way to say to app's user that server doesn't respond. 我想知道是否存在某种避免它使应用程序崩溃的方法,以及向应用程序的用户说服务器没有响应的某种方法。 I'm sorry if is there some silly mistake, i'm just new in Java. 如果有一些愚蠢的错误,对不起,我只是Java的新手。 The code: 编码:

        public void onClick(DialogInterface dialog, int whichButton) {

            InetAddress ia;
            try {

                ia = InetAddress.getByName("192.168.3.101");
                s = new Socket(ia,12346);
                Thread t = new Thread(new doComms(s));
                t.start();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }});

And the class doComms: 和doComms类:

class doComms implements Runnable {

        private Socket s;
        String line,inp;

        doComms(Socket server) {
          this.s=server;
        }

        public void run () {

          inp="";

          try {
            // Get input from the client
            DataInputStream in = new DataInputStream (s.getInputStream());
            PrintStream out = new PrintStream(s.getOutputStream());
            out.println(input.getText().toString()+"\n.");


            while((line = in.readLine()) != null && !line.equals(".")) {
              inp=inp + line;
            }

            runOnUiThread(new Runnable() {
                public void run() {
                    if(inp.equals("ok")){
                        Toast.makeText(Inicio.this,"Mensagem enviada com sucesso",Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(Inicio.this,"Falha ao enviar mensagem",Toast.LENGTH_LONG).show();
                    }
               }
           });

            Log.v("inp", inp);
            Log.v("type",inp.getClass().toString()); 

            s.close();


          } catch (IOException ioe) {
            System.out.println("IOException on socket listen: " + ioe);
            ioe.printStackTrace();
          }
        }

    }

You're using println() to send a line that already contains a newline. 您正在使用println()发送已经包含换行符的行。 That will result in two newlines being sent, which will probably confuse the peer and possibly cause it to return something unexpected, which your code may not be coping with correctly. 这将导致发送两个换行符,这可能会使对等方感到困惑,并可能导致其返回意外的内容,而您的代码可能无法正确处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM