0

This application supposed to send and recieve custom type objects that implements interface ContextObject

This object contains the hashmap changes (the problem) , whenever i change it after first .write it doesnt get changed at all.

Server :

      TestingSession(Socket socket) throws Exception {

            try {
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
    
                this.socket = socket;
    
                while (!socket.isClosed()) {
                    // reads an object of Type contextObject 
                    ContextObject receivedCO =(ContextObject)  objectInputStream.readObject(); 
    


                     // if its of type UpdateContextObject , then it contains a "changes" hashmap, print it. 
                    if(receivedCO instanceof  UpdateContextObject) {
                        UpdateContextObject ob= (UpdateContextObject) receivedCO;
                        Map<String, String> map = ob.getChanges();
                        for (Map.Entry entry : map.entrySet())
                            logger.trace(entry.getKey() + "  " + entry.getValue());
                    }



                        // send back the results, (not important in this question.)   
                    List queryResult = Query.processQuery(receivedCO);
                    objectOutputStream.writeObject(queryResult);

                }

Client :

            Socket socket;
            socket = new Socket("127.0.0.1", 5050);

            ObjectInputStream objectInputStream = new 
            ObjectInputStream(socket.getInputStream());
            ObjectOutputStream objectOutputStream = new 
            ObjectOutputStream(socket.getOutputStream());


            UpdateContextObject updateCO = new UpdateContextObject();


            // not important, for later processing.
            updateCO.setTable("users");
            updateCO.setAttributes(new HashMap<>());


             // This map that doesnt get changed.
            Map<String,String> changes = new HashMap<>();
            changes.put("Id","1");
            updateCO.setChanges(changes);


            int i=0;
            while(true){
                i++;

                changes.put("Id",""+i);


                objectOutputStream.writeObject(updateCO);
                objectInputStream.readObject();

                changes.put("username","3");
                System.out.println(changes.get("Id"));
                if(i>50) break;
            }

Server output :

setting Id to 1

setting Id to 1

setting Id to 1

setting Id to 1

setting Id to 1

Client output:

1 2 3 4 5 ..

Community
  • 1
  • 1
user2962142
  • 1,916
  • 7
  • 28
  • 42

0 Answers0