Search code examples
javasocketsserializationobjectoutputstreamobjectinputstream

server thread could not see changed fields of a serialized object


I'm facing with a problem about sending and receiving serialized object via TCP sockets. Actually, i can receive/send an object properly between a server thread and client thread.However, the issue is if a changed a property's value of a received/send object ,this change couldn't be realized by the waiting thread. Consider this code sample;

public class ClientThread extends javax.swing.JFrame implements Runnable {

ClientObject mainClient; // Initiliazed after sockets connect to server successfully
.
.
.
      String addNewBuddy = JOptionPane.showInputDialog(this, "Enter the Username of the person who you want to add...");

      mainClient.setBuddyRequest(true);
      mainClient.setBuddyRequestAccount(addNewBuddy);

      send.writeObject(mainClient); // write into an ObjectOutputStream
      send.flush(); // flush it

      System.out.println("mainClient.setBuddyRequest : " + mainClient.isBuddyRequest() + " setBuddyRequestAccount : " + mainClient.getBuddyRequestAccount()); // Check if values changed properly

      ClientObject tempClientObject; // temporary an instance of ClientObject

      while(( tempClientObject = (ClientObject) receive.readObject()) != null){

           if( !tempClientObject.isBuddyRequest() ){

                    JOptionPane.showMessageDialog(this, "Buddy Request Information", "Requested buddy doesnt exist!!!", JOptionPane.ERROR_MESSAGE);
                    break;
                }

                else{
                    JOptionPane.showMessageDialog(this, "Buddy Request Information", "Requested buddy added into your buddy list succesfully", JOptionPane.INFORMATION_MESSAGE);
                    labelSetText = tempClientObject.getNickName();
                    onlineStatus = tempClientObject.isIsOnline();
                    model.addElement(createPanel());
                }

            }
.
.
.
}

So after i changed some properties of mainClient i send it to server. Here is the part which server thread waits an object to give some reaction. Moreover, when client sends second object (which makes counter bigger than 0) server thread can read it without errors but i recognize that even client send a modified object as a second message to server there are no differences between properties of first and second object!.

        while( ( clientO = (ClientObject) receive.readObject()) != null ){

                counterMessage++;

                 if( counterMessage==1) { // 

                     checkAccountIfExist(toWrite,file.exists(),toModify,clientO); // Check is connected account exist in database of server

                 } // end of if (counter==1)

                 else{ // Second time when server waits 

// prints counter=2 but clientO.isBuddyRequest printed as 'false' 
//instead of 'true' so this makes if statement unreachable!
                     System.out.println("Counter = " + counterMessage + "  BUDDYREQUEST : " + clientO.isBuddyRequest() + " USERNAME : " + clientO.getUserName());

                     if(clientO.isBuddyRequest()){
                         System.out.println("Entered");
                         checkBuddyAvalaible(clientO);
                     }

                 }

        }

and finally my serializlible ClientObject's code

public class ClientObject implements Serializable {

    private static final long serialVersionUID = 8662836292460365873L;
    private String userName;
    private String password;
    private String nickName;
    private String message;
    private boolean checkAcc;
    private LinkedList<ClientObject> buddyList;
    private boolean isOnline;
    private boolean buddyRequest;
    private String buddyRequestAccount;

    public ClientObject(String userName, String password){

        this.userName = userName;
        this.password = password;
        this.checkAcc = false;
        this.buddyList = new LinkedList<ClientObject>();
        this.isOnline = false;
        this.buddyRequest = false;
        this.buddyRequestAccount = null;
    }

   ...methods of getters and setters
}

I hope i had been clear about the issue and i will appreciated for every answer, well thanks anyway.


Solution

  • All you need to do is call ObjectOutputStream.reset(), or use writeUnshared().