Situation : A client sends an object over to the server using ObjectOutputStream. I flush() and reset() after sending.
The server receives and object, does some processing and sends it back to the client. Immediately after sending, I call ObjectOutputStream.reset(). Prior to sending, I also print a field in this object in the console.
The client receives the same object back from the server using ObjectInputStream().
Problem : After receiving the object back at the client, the field that printed fine at the Server's console now prints NULL.
What I've tried :I use flush() and reset() after sending and receiving at both ends.
Why is the received object corrupted ?
Client code -- run() of Thread:
objectOutputStream.flush();
objectOutputStream.reset();
//out.println("#SerialO#");
objectOutputStream.writeBoolean(true);
if(((Dodger) this.obj).getRemotable()){
Log.i("ENDPOINT", "Sending Serialized ObjecInstance ...");
objectOutputStream.writeObject(this.obj);
objectOutputStream.flush();
objectOutputStream.reset();
}
Client code -- Function called TO receive Server's O/P
objectOutputStream.writeBoolean(false);
objectOutputStream.flush();
objectOutputStream.reset();
objectOutputStream.writeObject(args);
objectOutputStream.flush();
objectOutputStream.reset();
res = objectInputStream.readObject();
......
else if (res instanceof Dodger)
{
res = (Dodger) res;
res3 = (Dodger)res;
Log.v("CLIENT", "Object received Dodger "+ctr);
}
else
if(res==null)
Log.v("CLIENT", "Object received is NULL "+ctr);
else
Log.v("CLIENT", "Object received of UNKNOWN Type "+ctr);
catch (Exception e)
{
Log.e("CLIENT receive error 3",e.toString());
}
// ** ERROR COMES AT THIS LINE. NULL VALUE IS PRINTED **
System.out.println("ENDPOINT GETPOSITION -> "+res3.getPosition());
Server code - main thread.
flg = mInput.readBoolean();//this.input.readLine().toString();
System.out.println("SERVER Received flg as "+flg);
if(flg){
Log.d("SERVER","Received an object");
sendObj = mInput.readObject();
//mOutput.flush();
CommunicationThread commThread = new CommunicationThread(this.clientSocket,it,sendObj);
new Thread(commThread).start();
}
else //if(flg.equalsIgnoreCase("P"))
{
Object[] recArgs = (Object[])mInput.readObject();
executor = Executors.newFixedThreadPool(1);
executor.submit(new updateUIThread(this.clientSocket, this.msg, recArgs));
//Temp
//executor.execute(new updateUIThread(this.clientSocket, this.msg, recArgs));
//break;
}
Server code - Communication 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 ");
mInput = new ObjectInputStream(new BufferedInputStream(this.clientSocket.getInputStream()));
}
{
System.out.println("Inside Server's Comm Thread 3 ");
if (obj instanceof Dodger) {
obj1 = (Dodger)obj;
//...
System.out.println("Dodger Object Type: " + obj.getClass().getName());
}
else {
System.out.println("Unexpected object type: " + obj.getClass().getName());
}
Server code - updateUIThread:
obj1.setPosition((Vec2)this.recArgs[0]);
// BELOW IT PRINTS THE PROPER VALUE, BEFORE SENDING BACK TO CLIENT
System.out.println("SERVER GETPOSITION "+obj1.getPosition());
mOutput.reset();
mOutput.writeObject(obj1);
mOutput.reset();