1

我目前正在开发一个 Android 应用程序,它将一个字符串和一个文件发送到在远程计算机上运行的 java 服务器应用程序。这个java服务器应用程序应该找到文件上的索引并发回这个索引的值(文件结构是:索引值。示例:1蓝色)文件在远程机器上正确发送和接收,我有一个方法查找文件上接收到的索引的值。但是当我试图将找到的值发送回手机时,我得到一个异常(关闭的套接字),但我没有关闭套接字或任何缓冲区。我不确定关闭的套接字是移动应用程序套接字还是 java 服务器应用程序套接字。我正在使用与发送接收相同的套接字(这是在 Android 上工作的方式)。将答案发送回电话是我的项目所缺少的,也是我需要帮助的。

客户端应用程序(Android 应用程序):

 private class HeavyRemProcessing extends AsyncTask<String, Void, String>
        {

            protected String doInBackground(String... urls) 
             {
                    begins = System.currentTimeMillis();

                            remoteExecution();

                ends= System.currentTimeMillis();
                procTime=ends-begins;
                aux= Long.toString(procTime);

               return aux;
             } //doInBackground() ends

            protected void onPostExecute(String time)
              {
                textView1.setText("Result: "+result+". Processing Time: "+time+" milisecs"); 
              }// onPostExecute ends

        } //HeavyRemProcessing ends


     public void executor(View view)
      {     
         key="74FWEJ48DX4ZX8LQ";

         HeavyRemProcessing task = new HeavyRemProcessing();
         task.execute(new String[] { "????" });     
      } //executor() ends


     public void remoteExecution()
        {
               // I have fixed IP and port I just deleted 
           String ip;  //SERVER IP
           int port;   // SERVER PORT 

             try
               {
                 cliSock = new Socket(ip, port);

                 file= new File("/mnt/sdcard/download/Test.txt"); 

                 long length = file.length();
                 byte[] bytes = new byte[(int) length];

                 FileInputStream fis = new FileInputStream(file);
                 BufferedInputStream bis = new BufferedInputStream(fis);
                 BufferedOutputStream out = new BufferedOutputStream(cliSock.getOutputStream());
                 BufferedReader in = new BufferedReader(new InputStreamReader(cliSock.getInputStream()));


                 int count;
                 key=key+"\r\n";
                 out.write(key.getBytes());
                 while ((count = bis.read(bytes)) > 0) 
                  {
                     out.write(bytes, 0, count);
                  }  //It works perfectly until here


                   //// PROBABLY HERE IS THE PROBLEM:               
                out.flush();
                out.close();
                fis.close();
                bis.close();                    

                result= in.readLine();  //RECEIVE A STRING FROM THE REMOTE PC


              }catch(IOException ioe)
                {
                 // Toast.makeText(getApplicationContext(),ioe.toString() +   ioe.getMessage(),Toast.LENGTH_SHORT).show();  
                } 

            }catch(Exception exp)
              {
                 //Toast.makeText(getApplicationContext(),exp.toString() +   exp.getMessage(),Toast.LENGTH_SHORT).show();               
              }   

        } //remoteExecution ends

Java 服务器应用程序(远程 PC)

 public void receivingFile()
      {
         System.out.println("Executing Heavy Processing Thread (Port 8888).");

         try 
            {
                serverSocket = new ServerSocket(8888);
                InputStream is = null;
                OutputStream os= null;
                FileOutputStream fos = null;
                BufferedOutputStream bos = null;
                BufferedOutputStream boSock =null;
                DataOutputStream dataOutputStream=null;
                int bufferSize = 0;


                try 
                   {
                     socket = serverSocket.accept();  
                     System.out.println("Heavy Processing Task Connection from ip: " + socket.getInetAddress());

                   } catch (Exception ex) 
                     {
                       System.out.println("Can't accept client connection: "+ex);
                     }

                try 
                   {
                     is = socket.getInputStream();
                     dataOutputStream = new DataOutputStream(socket.getOutputStream());

                     bufferSize = socket.getReceiveBufferSize();

                   }
                     catch (IOException ex) 
                       {
                         System.out.println("Can't get socket input stream. ");
                       }

                try 
                   {
                     fos = new FileOutputStream(path);
                     bos = new BufferedOutputStream(fos);

                   }
                     catch (FileNotFoundException ex) 
                      {
                        System.out.println("File not found. ");
                      }

                byte[] bytes = new byte[bufferSize];

                int count;
                System.out.println("Receiving Transfer File!.");

                while ((count = is.read(bytes)) > 0) 
                  {
                     bos.write(bytes, 0, count);
                  }

                System.out.println("File Successfully Received!.");
                fos.close();
                bos.flush();
                bos.close();
                is.close();

                result= obj.searchIndex();
                System.out.println("Found: "+result); //This correctly print the found value

                dataOutputStream.writeUTF(result);
                dataOutputStream.flush();
                dataOutputStream.close();

                System.out.println("Data sent back to the Android Client. ");


           } catch (IOException e) 
              {
               // TODO Auto-generated catch block
               e.printStackTrace();
              }

      } // receivingFile() ends

如果有人可以帮助我,我将非常感激。我在想可能与缓冲区和套接字有关。我的 Java 服务器应用程序抛出异常:“关闭的套接字”...感谢您的时间,

阿尔贝托。

4

1 回答 1

1

我认为您的问题是您outputstream在关闭inputstream. 这是android中的一个错误。通常在java中关闭outputstream刷新数据,关闭输入会导致连接关闭。

但是在android中关闭outputstream关闭连接。这就是为什么你得到关闭的套接字异常,
把语句

out.flush();
 out.close();


result=in.readLine();

或者只是避免那些陈述(out.flush 和 out.close)。我也遇到过类似的问题。看我的问题

于 2012-10-30T08:54:30.987 回答