Search code examples
javasocketsserializationobjectinputstreamobjectoutputstream

how to update class by socket


enter image description here

// CLIENT SOURCE

while(!isStop){
    try{
        Object ob = mc.getOis().readObject();
        try{ 
            message = (String) ob;
        } catch (ClassCastException e){

            try {
                user = (User) ob;
                mc.user = (User) ob;
                message = "none";
            } catch ( ClassCastException ee ) {

                Room room = (Room) ob;
                mc.setRoom(room);   
                System.out.println("room come in");
                for (int i = 0; i<mc.room.userlist.size(); i++ )
                {
                    System.out.println("ThreadUserNum:" + room.userlist.get(i).getPlayer_Num());
                }


                mc.roomidimg();
                message = "none";

            }   
        }
    }
}

// SERVER SOURCE
public void roombroodcast ( Room msg, int room_id) throws IOException{
    if (room_id == 1){
        for( ServerThread ct : sv.getroom1list() ){
            for( int i = 0; i<msg.getUserlist().size(); i++){
                System.out.println(i + "broodcast:" + msg.getUserlist().get(i).getUser_Id());
            }
            ct.send( msg );
        }
    } else {
        for( ServerThread ct : sv.getroom2list() ){
            ct.send( msg );
        }
    }
}

We are making a game but we have some problems. The first user, who entered the game first, cannot get the other user's room class broadcasting. I mean, the other user or another user can get the room class broadcasting but the first one cannot get the updated room information. How can I update the room class broadcasting problem? We are using sockets to communicate.


Solution

  • You are always sending the same object, so serialization will not actually re-send it: it economizes, as described in the Object Serialization Specification. So updating it and sending it again will not affect the object received at the receiver.

    You need to investigate ObjectOutputStream.reset() or ObjectOutputStream.writeUnshared().