在socket关闭之前,OutputStream不会发送

时间:2015-05-14 15:51:36

标签: android sockets outputstream

您好我正在开发一个使用套接字控制计算机的Android应用程序,但我的问题是我从服务器获取数据,直到我关闭输出流因此关闭套接字并且无法通过再发送或接收数据。我的问题是如何在不关闭OutputStream的情况下将数据从服务器发送到客户端?

此代码正在关闭OutputStream(ps1.close()):socket已关闭

private class Receivecmd extends Thread{
        Scanner sc = null;
        @Override
        public void run() {
            Runtime r;
            try {
                sc = new Scanner(socket.getInputStream());
                while(true)
                {
                    if(sc.hasNext())
                    {
                        System.out.println("Got Some");
                        String sr = sc.nextLine();
                        if(sr.contains("shutdown "))
                        {
                            if(sr.contains("/h"))
                            {
                                sr = sr.replace("/t", "");
                            }
                            r = Runtime.getRuntime();
                            r.exec(sr+"/f");
                        }
                        else if (sr.contains("taskman"))
                        {
                            System.out.println("Task");
                            r = Runtime.getRuntime();
                            Process p = r.exec("tasklist.exe");
                            Scanner sc1 = new Scanner(p.getInputStream());
                            PrintStream ps1 = new PrintStream(socket.getOutputStream());
                            while(sc1.hasNext())
                            {
                                String s = sc1.nextLine();
                                ps1.println(s);
                                System.out.println(s);

                            }
                            ps1.close();

                        }
                        else if(sr.contains(".exe"))
                        {
                            r = Runtime.getRuntime();
                            r.exec(sr);
                            System.out.println(sr);
                        }


                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(AndroidServer.class.getName()).log(Level.SEVERE, null, ex);
            }
            super.run(); //To change body of generated methods, choose Tools | Templates.
        }

当我删除ps1.close()时,此代码无效:我仍然可以接收数据

private class Receivecmd extends Thread{
        Scanner sc = null;
        @Override
        public void run() {
            Runtime r;
            try {
                sc = new Scanner(socket.getInputStream());
                while(true)
                {
                    if(sc.hasNext())
                    {
                        System.out.println("Got Some");
                        String sr = sc.nextLine();
                        if(sr.contains("shutdown "))
                        {
                            if(sr.contains("/h"))
                            {
                                sr = sr.replace("/t", "");
                            }
                            r = Runtime.getRuntime();
                            r.exec(sr+"/f");
                        }
                        else if (sr.contains("taskman"))
                        {
                            System.out.println("Task");
                            r = Runtime.getRuntime();
                            Process p = r.exec("tasklist.exe");
                            Scanner sc1 = new Scanner(p.getInputStream());
                            PrintStream ps1 = new PrintStream(socket.getOutputStream());
                            while(sc1.hasNext())
                            {
                                String s = sc1.nextLine();
                                ps1.println(s);
                                System.out.println(s);

                            }


                        }
                        else if(sr.contains(".exe"))
                        {
                            r = Runtime.getRuntime();
                            r.exec(sr);
                            System.out.println(sr);
                        }


                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(AndroidServer.class.getName()).log(Level.SEVERE, null, ex);
            }
            super.run(); //To change body of generated methods, choose Tools | Templates.
        }

3 个答案:

答案 0 :(得分:0)

使用flush()

  

通过将任何缓冲的数据写出到底层输出来刷新对象。

答案 1 :(得分:0)

接收器可能会逐行处理输入,因此您应该追加一个。

        var i = 0;
        Array.prototype.forEach.call(response.albumList2.album, function (e) {
          // buildObject returns the needed data from the object that is returned from the loop as well as other needed data
          var obj = this.buildObject(e);
          // this.wall is the array that i pass to core-list to display
          this.wall.push(obj);
          // this is to keep callback from executing before the array is built
          i = i + 1;
          if (i === response.albumList2.album.length) {
            this.async(callback);
          }
        }.bind(this));

答案 2 :(得分:0)

  

我使用此扫描程序将从输入流中捕获的所有行放入名为tasklist的字符串arraylist

换句话说,您在输入流之前一直在读取输入,这只会在您关闭或关闭发送方的套接字时发生。

您可以将其关闭以进行输出,但这意味着您只能执行其中一个请求/响应周期。您需要重新考虑您的应用程序协议。或者只是一次处理一个任务。