Android:将散布在参数的序列化对象发送到服务器



情况::我必须将参数发送到服务器或序列化对象。它可以按任何随机顺序。

我所做的:根据这篇文章,我开发了一个协议,让服务器知道在实际发送之前发送的内容。如果是任何字符串,则仅通过printwriter.println()进行。

但是,在通过ObjectInputStream发送序列化对象之前,我会发送printWriter.println(" O")。因此,服务器知道它必须期望接下来一个序列化对象。

问题:我正在获取StreamCorreutDexception,即使我在整个插座的一生中仅使用一个objectInputStream的实例。服务器端应该做任何事情吗?

-----------------------------------------------------------------------------------------------------------------------------------------------/p>

客户端结束:

                PrintWriter out = new PrintWriter(new BufferedWriter(
                new OutputStreamWriter(thisSocket.getOutputStream())),
                true);
                if (!sent){
                    out.println("Dummy");
                    sent = true;
                }
                objectOutputStream.flush();
                objectOutputStream.reset();
                out.println("#SerialO#"); //This param signals the server
                                          //that an object will be sent next.
                if(((calcSum) this.obj).getRemotable()){
                    /*objectOutputStream.flush();
                    objectOutputStream.reset();*/
                        Log.i("ENDPOINT", "Sending Serialized data ...");
                        objectOutputStream.writeObject(this.obj);
                        objectOutputStream.flush();
                        objectOutputStream.reset();
                        // Get the byte array
                        byte[] byteArray = byteArrayOutputStream.toByteArray();
                }

服务器结束:

while (!Thread.currentThread().isInterrupted()) {
                try{
                    this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
                    // NOW DECLARED AND USED IN UpdateUIThread
                    //PrintStream out = new PrintStream(socket.getOutputStream());
                    if(!sent)
                    {
                        flg = this.input.readLine().toString();
                        Log.i("SERVER","SERVER Received False flg as "+flg);
                        sent = true;
                    }
                    else{
                    flg = this.input.readLine().toString();
                    System.out.println("SERVER Received flg as "+flg);
                    if(flg.contains("#SerialO#")){  //Be prepared to
                                              // receive a serilized obj from client
                        //Log.d("SERVER","Received an object");
                        CommunicationThread commThread = new CommunicationThread(this.clientSocket,it);
                        new Thread(commThread).start();
                    }
                    else
                    {
                        //Log.d("SERVER","Received a parameter "+flg);
                        executor = Executors.newFixedThreadPool(5);
                        executor.execute(new updateUIThread(this.clientSocket,flg));
                    }
                  }
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e("SERVER", "Creation went bad -- "+e.toString());
                    break;
                }
                catch (Exception e)
                {
                    Log.d("CLIENT TAG", "Creation went bad -- "+e.toString());  
                }
            }
        }

...
public CommunicationThread(Socket clientSocket, Intent it) {
            this.clientSocket = clientSocket;
            try {
                Log.d("SERVER","Inside Server's Comm Thread");
                if (mInput==null){
                    Log.i("SERVER","Receiving very first serialized obj");
                    mOutput = new ObjectOutputStream(this.clientSocket.getOutputStream());
                    System.out.println("Inside Server's Comm Thread 2 ");
// EXCEPTION OCCURS IN THE LINE BELOW.
                    mInput = new ObjectInputStream(new BufferedInputStream(this.clientSocket.getInputStream()));
                }

,如果其中任何一个被缓冲,则不能在同一插座上混合流和读者/作家。在这种情况下,我将使用对象流进行所有内容。

相关内容

  • 没有找到相关文章

最新更新